Why does this only print arg
out once?
$ alias test="echo $1 $1 $1"
$ test arg
arg
This is on GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu).
List aliases with alias
command after you defined that alias and you will clearly understand it:
alias test="echo $1 $1 $1"
alias
Output:
alias test='echo '
It simply expands to a string with variables only which were defined at execution time. So this for example works (Vvariable MY_VAR is defined):
MY_VAR="foo"
alias test="echo $MY_VAR $MY_VAR $MY_VAR"
alias
Output:
alias test='echo foo foo foo'
You can replace it with function
function test() {
echo $1 $1 $1
}
alias est='echo 1${1} var${var}'
. The problem then is $1
is still not populated as the OP expects. The function is the solution.
I doubt if alias
supports $1,$2,... Its help keeps silence on that.
So I think that your command test arg
is expanded like echo $1 $1 $1 arg
. Where $1 is empty and you get your result.
1 more example:
$ alias test='echo $1 $1 $1'
$ test arg1 arg2
arg1 arg2
In the above example the alias test=...
get's executed and the net effect is we're let with an alias to the command echo
. We can see this effect when we run the command test arg1 arg2
. All we're doing is running echo
and passing it the strings arg1
and arg2
.
$ echo arg1 arg2
test=echo
. And you're test arg1 arg2
is really doing echo arg1 arg2
.