LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-17-2013, 10:49 AM   #1
moraxu
LQ Newbie
 
Registered: Dec 2012
Distribution: Ubuntu
Posts: 15

Rep: Reputation: Disabled
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.
 
Old 02-17-2013, 11:09 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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.

Last edited by shivaa; 02-17-2013 at 11:14 AM.
 
Old 02-17-2013, 11:28 AM   #3
moraxu
LQ Newbie
 
Registered: Dec 2012
Distribution: Ubuntu
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
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.
 
Old 02-17-2013, 02:59 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by moraxu View Post
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 View Post
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to escape bash-special character in a bash string? fantasy1215 Programming 12 03-10-2012 09:45 AM
BASH -le parameter hattori.hanzo Linux - Newbie 5 01-17-2009 02:17 AM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM
bash parameter indirection possible? kornelix Programming 5 11-30-2005 08:35 AM
Bash function parameter Misel Programming 2 05-17-2003 11:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration