EverythingRightPlace
EverythingRightPlace

Reputation: 1197

Why is this alias including a parameter working?

Since variables are problematic when specifying an alias in Bash (in the .bashrc), something like the following isn't working:

alias count1="num=$1 | cat $num | wc"
alias count2="`cat $num` | wc"

Why is

alias killID="kill -9 `pgrep $1`"

working then?

Btw. when searching about this I learned that we should always use functions in the .bashrc to define something like the above. Just made me curious.

edit

Other example which isn't working with a parameter (because my first example is bad):

alias testalias='du -m ./* | sort -nr | head -n $1 ; du -sh'

Upvotes: 0

Views: 65

Answers (1)

cdarke
cdarke

Reputation: 44394

In the first cases you are using pipes, when you should be using a group.

Also, you are using the wrong kind of quotes, you should be using single quotes, not double. Using double quotes will give the value of variables at the time you declare the alias, not at the time you run the command.

These are also examples of useless use of cat. See http://porkmail.org/era/unix/award.html

Example:

alias count1='num=$1;wc $num'

In the command that works the $1 expansion is never used. It is not stored in the alias. The parameter (being on the right) is appended to the constructed command at runtime. Check the constructed alias with the alias command to see what it really does.

Upvotes: 1

Related Questions