James Zuchelli
James Zuchelli

Reputation: 21

single quotes not working in shell script

I have a .bash_profile script and I can't get the following to work alias lsls='ls -l | sort -n +4'

when I type the alias lsls it does the sort but then posts this error message "-bash: +4: command not found" How do I get the alias to work with '+4'?

It works when type ls -l | sort -n +4 in the command line

I'm in OS X 10.4

Thanks for any help

Upvotes: 2

Views: 328

Answers (3)

wich
wich

Reputation: 17157

bash-4.0$ ls -l | sort -n +4
sort: open failed: +4: No such file or directory

You need ls -l | sort -n -k 5, gnu sort is different from bsd sort

alias lsls='ls -l | sort -n -k 5'

Edit: updated to reflect change from 0 based indexing to 1 based indexing, thanks Matthew.

Upvotes: 3

Matthew Slattery
Matthew Slattery

Reputation: 47058

alias lsls='ls -l | sort -n +4' should work fine with the sort in OS X 10.4 (which does support that syntax).

when I type the alias lsls it does the sort but then posts this error message "-bash: +4: command not found"

Is it possible that you inserted a stray newline when editing your .bash_profile? e.g. if you ended up with something like this:

alias lsls='ls -l | sort -n
+4'

...that might explain the error message.


As an aside, you can get the same effect without piping through sort at all, using:

ls -lrS

Upvotes: 1

pavium
pavium

Reputation: 15128

This link discusses a very similar alias containing a pipe.

The problem may not have been the pipe, but the interesting solution was to use a function.

Upvotes: 0

Related Questions