Skip to content
Advertisement

Is it possible to get command arguments now executing?

I want to declare aliases which append argument if no argument specified.

For example,

alias vimtutor=‘vimtutor $([ $# -eq 0 ] && echo ja.utf-8)’

I expected executing vimtutor with no argument is replaced to vimtutor ja.utf-8, but it doesn’t work properly.

So, I tried some tests on bash shell.

$ echo $#
0
$ echo a b c $#
a b c 0
$ echo $0
-bash

And I understood it is impossible to get command arguments now executing in normal way.

Now, I declare shell function showing below,

vimtutor(){ /usr/bin/vimtutor $([ $# -eq 0 ] && echo ja.utf-8) “$@“; }

It woks as expected but feel not smart way.

Is there any idea to solve this problem?

Advertisement

Answer

The variables $#, $0, etc when used in an alias references your bash session variables; the arguments used when your bash session was started, not the arguments passed to the alias command.

I suggest you put your command in a script, and alias to that script. I.e.

$ echo 'vimtutor $([ $# -eq 0 ] && echo ja.utf-8)' > ~/vimtutor.sh
$ chmod +x ~/vimtutor.sh
$ alias vimtutor='~/vimtutor.sh'
Advertisement