Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Pretty new to bash scripting. Trying to over ride the ls -la command to be: ls -la | more

(Seems more useful for me).

I added this to the end of my .bashrc.

154 # alias 'ls -la'='ls -la | more'
155         # this did not work because aliases
156         # are not allowed to have spaces in
157         # them. => have to make function:
158 ls() {
159         if [[ $@ == "-la" ]];
160         then
161                echo "test";
162                 command ls -la | more;
163         else
164                 command ls "$@";
165         fi;
166 }

But I get this error when I open a new terminal:

bash: /users/me/.bashrc: line 158: syntax error near unexpected token `('
bash: /users/me/.bashrc: line 158: `ls() {'

When I add function before ls() { there are no complaints but there is no change to ls behavior. Thanks.


Trying to narrow down the issue I did this:

159 ls() {
160         #if [[ $@ == "-la" ]];
161        # then
162                echo "test"
163 #               command ls -la | grep vim;
164         #else
165         #        command ls "$@";
166         #fi;
167 }

but I still get the same error. I think that the main problem may be that it is in the bashrc file?


Strangely this works

159 function ls() {
160         #if [[ $@ == "-la" ]];
161        # then
162                echo "test"
163 #               command ls -la | grep vim;
164         #else
165         #        command ls "$@";
166         #fi;
167 }
share|improve this question
up vote 1 down vote accepted

I can't reproduce this on Bash-3.00.16 on Solaris 10 or Cygwin, but it looks like your bash is parsing line 158 as an invocation of the ls command, and not as a function definition.

The "function" prefix is optional, but if it makes your bash accept the definition, then it's a reasonable workaround. It doesn't alter the meaning of the function in any way.

Incidentally, which OS and Bash version are you using ?

uname -a
bash --version
share|improve this answer
Running solaris 10 with the same bash as you. My assumption was that it was just a mistake with the spaces or something like that. Or maybe brackets. Did you do a direct copy-paste of my code? And it worked? Thanks for the help! – sixtyfootersdude Feb 8 '10 at 16:53
Yes, I copied and pasted it exactly into my .bashrc, removed line numbers, and no error was reported. – njd Feb 8 '10 at 17:28
very strange.. No idea what the error suggests? – sixtyfootersdude Feb 8 '10 at 17:36
Ok that is mostly working now. Did the if statement ever evaluate to true for you? Ie: did ls -la | more ever execute? – sixtyfootersdude Feb 8 '10 at 17:50
HA! I have an alias that takes ls and maps it to ls -F and that was messing things up. Problem solved. – sixtyfootersdude Feb 8 '10 at 18:03

I realize this isn't exactly what you're asking about, but I mention it in case you've not thought about this sort of solution. In my experience, the usual way to work around this is to make an alias without a space, e.g.:

alias llm="ls -la | more"

This is a lot quicker and easier than writing a script. FWIW, I tested this on a FreeBSD 5.3 system running Bash 3.0 and it provided a listing run through more.

share|improve this answer
Thanks for the comment. Thought about that but prefer the script solution. – sixtyfootersdude Feb 8 '10 at 17:25

replace with:

 function ls
         if [[ $@ == "-la" ]]
                echo "test"
                /bin/ls -la | more
                /bin/ls "$@"

Note: you'll need to reference the absolute path to the actual ls command inside the if or that'll reinvoke the same function and.... stackoverflow! :-)

EDIT: Yes, the command key word is a better way of preventing alias lookups.

share|improve this answer
I am pretty sure that is what the command keyword does? – sixtyfootersdude Feb 8 '10 at 17:26
Using the 'command' command (!) prevents the function/alias lookup. – njd Feb 8 '10 at 17:32
@njd: thanks for clarification. – sixtyfootersdude Feb 8 '10 at 17:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .