Bash: overridding ls -la with ls -la | more (basic bash scripting problem)

0

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.

Update

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?

Update

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 }

sixtyfootersdude

Posted 2010-02-08T16:13:50.640

Reputation: 6 399

Answers

1

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

njd

Posted 2010-02-08T16:13:50.640

Reputation: 9 743

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 – 2010-02-08T16:53:55.677

1Yes, I copied and pasted it exactly into my .bashrc, removed line numbers, and no error was reported. – njd – 2010-02-08T17:28:58.650

very strange.. No idea what the error suggests? – sixtyfootersdude – 2010-02-08T17:36:22.577

Ok that is mostly working now. Did the if statement ever evaluate to true for you? Ie: did ls -la | more ever execute? – sixtyfootersdude – 2010-02-08T17:50:03.810

HA! I have an alias that takes ls and maps it to ls -F and that was messing things up. Problem solved. – sixtyfootersdude – 2010-02-08T18:03:55.070

D'oh! The simplest solutions are always the best. Nice to know you got it fixed in the end. – njd – 2010-02-08T18:54:45.737

4

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.

GreenMatt

Posted 2010-02-08T16:13:50.640

Reputation: 835

Thanks for the comment. Thought about that but prefer the script solution. – sixtyfootersdude – 2010-02-08T17:25:22.697

1

replace with:

 function ls
 {
         if [[ $@ == "-la" ]]
         then
                echo "test"
                /bin/ls -la | more
         else
                /bin/ls "$@"
         fi;     
}

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.

SuperMagic

Posted 2010-02-08T16:13:50.640

Reputation: 935

I am pretty sure that is what the command keyword does? – sixtyfootersdude – 2010-02-08T17:26:02.430

1Using the 'command' command (!) prevents the function/alias lookup. – njd – 2010-02-08T17:32:29.123