LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   The $_ bash special parameter (https://www.linuxquestions.org/questions/linux-newbie-8/the-%24_-bash-special-parameter-4175450545/)

moraxu 02-17-2013 10:49 AM

The $_ bash special parameter
 
I've 2 questions regarding the way the $_ bash special parameter works. According to the bash man page:

Quote:

(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:

Code:

$ cat my_script
echo "\$_ = $_, \$0 = $0"
$ bash my_script
$_ = /bin/bash, $0 = my_script
$ ./my_script
$_ = ./my_script, $0 = ./my_script
$ ~/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:

Code:

$ bash #non-login
$ echo $_
/usr/share/bash-completion/bash_completion

$ 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?

Quote:

Also set to the full pathname used to invoke each command executed and placed in the environment exported to that command.

shivaa 02-17-2013 11:09 AM

Quote:

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:
Code:

~$ bash <scriptname>.bash
Will simply invoke a bash script, whereas invoking only bash without any argument will change your working shell to bash.

Second thing, a shell script will follow it's execution path according to the shebang mentioned in it, that is:-
Code:

#!/bin/bash
This is the path where all your remaining script will follow for execution.

On the other hand, invoking a script like:
Code:

~$ ./<scriptname>.bash
Will be successful only when your login shell and shebang are same. Else it will give your error during execution.

Well, your question is little confusing to me, so be little specific and beiefly mention that what exactly you want to understand.

moraxu 02-17-2013 11:28 AM

Quote:

Originally Posted by shivaa (Post 4893714)
Well, your question is little confusing to me, so be little specific and beiefly mention that what exactly you want to understand.

I'd like to know why these echo outputs:

Code:

$ bash #non-login
$ echo $_
/usr/share/bash-completion/bash_completion

$ bash --login
$ echo $_
]

are not /bin/bash.

ntubski 02-17-2013 02:59 PM

Quote:

Originally Posted by moraxu (Post 4893726)
I'd like to know why these echo outputs:
Code:

$ bash #non-login
$ echo $_
/usr/share/bash-completion/bash_completion

$ bash --login
$ echo $_
]

are not /bin/bash.

You are starting up a subshell. The values for $_ are from the startup files bash executes. Exit the subshell and you will have the output you expect:
Code:

~/tmp$ /bin/bash
~/tmp$ echo $_
histexpand
~/tmp$ exit
exit
~/tmp$ echo $_
/bin/bash

Quote:

Originally Posted by shivaa (Post 4893714)
On the other hand, invoking a script like:
Code:

~$ ./<scriptname>.bash
Will be successful only when your login shell and shebang are same. Else it will give your error during execution.

That is false, your login shell is irrelevant for this case.


All times are GMT -5. The time now is 07:55 AM.