jasir
jasir

Reputation: 1471

Bash - getting strange arguments when using $*

When i define alias like:

alias strange="echo $*"

then

strange one two three

outputs:

completion-ignore-case on one two three

similary, for this alias:

alias strange2="echo $1 $2 $3 $4"
strange2 one two three four
completion-ignore-case on one two three four

I am on Windows, using git-bash... Any ideas why is this happening?

Upvotes: 1

Views: 35

Answers (2)

chepner
chepner

Reputation: 531205

Aliases are intended for pure text expansion; they are not parameterized. However, you can simulate a limited form of parameter passing by using single quotes; this defers the expansion of $* until the alias is expanded, although it requires you to set the positional parameters manually before using the alias.

$ alias strange='echo $*'
$ set a b c
$ strange
a b c
$ set d e
$ strange
d e

Any "arguments" passed to the use are simply appended to the end of the alias expansion; that is, strange 1 2 3 is first expanded to echo $* 1 2 3, which then undergoes the normal shell processing; the expansion of $* is unrelated to the following words.

For true parameterization, use a shell function instead.

Upvotes: 1

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

Using double quotes doesn't prevent expansion to take place when setting the alias indeed strange will be aliased to echo $* where $* is replaced with current shell arguments. Use single quotes to prevent expansion, and use alias strange to see current alias definition.

Upvotes: 1

Related Questions