I've 2 questions regarding the way the $_
bash special parameter works. According to the bash man page:
(An underscore.) At shell startup, set to the absolute pathname used to invoke the shell or shell script being executed as passed in the environment or argument list. Subsequently, expands to the last argument to the previous command, after expansion. Also set to the full pathname used to invoke each command executed and placed in the environment exported to that command.
1. We have the following session:
$ cat my_script
echo "\$_ = $_, \$0 = $0"
$ bash my_script
$_ = /bin/bash, $0 = my_script
$_ = ./my_script, $0 = ./my_script
$_ = /home/moraxu/my_script, $0 = /home/moraxu/my_script
And everything's clear so far, if we run a shell script directly, $_
is simply equivalent to $0
However, if we don't supply a shell script as the argument to bash, my output will be different, depending on whether the shell is a login shell or not:
$ bash #non-login
$ echo $_
$ bash --login
$ echo $_
Why it isn't just /bin/bash
as in the first snippet?
2. Could you give me an example of the situation this quote refers to?
Also set to the full pathname used to invoke each command executed and placed in the environment exported to that command.