views:

88

answers:

3

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

+4  A: 
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.

wich
That's not the "+4: command not found" error that was being reported. The `sort` in OS X 10.4 supports both syntaxes. And the equivalent of `+4` would be `-k 5`.
Matthew Slattery
@Matthew And you are right, seems +4 is supported in OS X 10.4, the alias worked perfectly for me on OS X 10.4.
wich
A: 

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.

pavium
The issue is not the pipe, the issue is that gnu sort takes different arguments; the +4 should be -k 4
wich
+1  A: 

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
Matthew Slattery
ls -lrS is a better answer, but sort -n +4 doesn't work on OSX 10.6.x at least
fuzzy lollipop
Right, but the original poster did specifically say "I'm in OS X 10.4". And I actually tried it.
Matthew Slattery