Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

bash alias with parameters [duplicate]

Writer Mia Lopez

I would like to swap from csh to bash, then I have to set the .bashrc with the commands I use. Translating alias with parameters seems to be not easier as I believed. csh:

alias gr 'xmgrace -legend load -nxy \!* -free -noask&'

the param \!* means all params on the command line; Then I tried for bash:

alias gr='xmgrace -legend load -nxy $@ -free -noask&'
alias gr='xmgrace -legend load -nxy $(@) -free -noask&'

But neither worked.

The other issue comes from memorizing the current directory csh:

alias t 'set t=\`pwd\``;echo $t'
alias tt 'cd $t'

I tried a lot of things but without any results.

4

1 Answer

This is not the way bash aliases work, all the positional parameters in the bash aliases are appended at the end of the command rather than the place you have defined. To get over it you need to use bash functions.

An example will make you more clear :

$ cat file.txt
foo
$ cat bar.txt
foobar
spamegg
$ grep -f file.txt bar.txt
foobar
$ alias foo='grep -f "$1" bar.txt' ## Alias 'foo'
$ foo file.txt
grep: : No such file or directory
$ spam () { grep -f "$1" bar.txt ;} ## Function 'spam'
$ spam file.txt
foobar

As you can see as the first argument in case of alias foo is being added at the end so the command foo file.txt is being expanded to :

grep -f "" bar.txt file.txt

while in case of function spam the command is correctly being expanded to :

grep -f file.txt bar.txt

So in your case, you can define a function like :

gr () { xmgrace -legend load -nxy "$@" -free -noask & ;}
3