2

I am trying below alias in cshell:

alias sll 'ls -l \!* | grep -oE '[^ ]+$' | paste -s | xargs ls -l'

For this CSH says, Illegal variable name.

If I use \$, alias will be set without any error. But when I use this alias, I get, grep: Invalid regular expression

PS:

  1. The aim of this alias is to achieve ls -Ll <filenames> but with full path of the file the symbolic link references.
  2. The RHS part of alias works fine as standalone command.
  3. Instead of egrep (grep -E), I tried awk '{print $NF}'. Even for this, CSH gives the error: NF undefined variable.
  • 1
    Does csh allow nested single quotes? I suspect not. Try changing the inner single quotes to doubles. – glenn jackman Jan 18 '16 at 14:42
  • Then the grep will try to interpret $ as a variable and hence fails. – BluVio Jan 19 '16 at 6:00
  • Does csh really do that? man... – glenn jackman Jan 19 '16 at 14:14
4

Check your single quotes. Single quotes don't magically nest.

alias sll 'ls -l \!* | grep -oE '\''[^ ]+$'\'' | xargs ls -ld --'

That's still flawed for several reasons:

  • Because of [^ ], that won't work for file or link target names that contain spaces.
  • as you're treating that list as a list of lines, that won't work with file/link target names that contain newline characters.
  • because of xargs, that won't work with file/target names containing apostrophes, backslashes, double quotes, other types of blanks.
  • For symlinks with relative paths as their targets, that only works for symlinks in the current directory as what you pass to the second ls is a relative path to the current directory, while symlinks are resolved relative to the path of the symlink file (if you have a a/b/c -> d link, that's to a/b/d, not d in the current directory).
  • you'll get a spurious error message because of the first total <n> line of the output of ls.
  • for symlinks to symlinks, that won't give you the same as ls -lL (may be what you want though).

With zsh, you could make it:

ls -ld -- *(:A)

The :A modifier expands symlinks to their canonical absolute path.

A quasi-equivalent on GNU systems:

readlink -fz -- * | xargs -r0 ls -ld --
  • Thanks Stéphane Chazelas for detailed answer. It works. With regards to flaws you mentioned, for my use case(s) >> Flaws 1,2,3 & 6 are not applicable. >> For flaw 5, i intend to either live with it or use sll * >> For flaw 4, this very much applies to my use case. I guess, I can't do it in a one liner and I have to write a script for that. – BluVio Jan 19 '16 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.