2

I have the following command which works:

ls -la | awk '$5 > 2'

I'm trying to alias the entire thing. If I try a naive way:

alias ll "ls -la | awk '$5 > 2'"

It doesn't work.

If I try to escape the dollar sign, I get only a part of the command and ll is now ls -la | awk $5

What should I do in order to define this alias the way I want it to be?

1
  • Use a function instead of alias, then you don't have to worry about quoting issues.
    – Barmar
    Commented Aug 25, 2016 at 19:53

2 Answers 2

1

Just use a shell function if your alias is more complicated than a simple command:

lz () {
    ls -la | awk '$5 > 2'
}

... or find a command that is simpler and fits as an alias:

alias lz='find . -maxdepth 1 -size +2c -ls'

The bash manual contains the statement

For almost every purpose, aliases are superseded by shell functions.

Related: Why *not* parse `ls`?

0

escape, equal

$ alias ll="ls -la | awk '\$5 > 2'"
$ alias|grep ll                                       
ll='ls -la | awk '\''$5 > 2'\'
4
  • That's not the resule i'm getting. The result i'm getting is ls -la | awk $5.
    – asaf92
    Commented Aug 25, 2016 at 15:04
  • Which shell do you use? I've tried it on mksh & bash. Commented Aug 25, 2016 at 15:21
  • @PanthersFan92: How do you check it? Commented Aug 25, 2016 at 15:22
  • 1
    There is no need to grep an alias. This alias ll is faster and simpler.
    – user184899
    Commented Aug 26, 2016 at 21:02

You must log in to answer this question.

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