11

I'm trying to understand how the environment variable _ can be used. Below is an example of using it:

$ echo $_

$ echo $_
echo

$ ls non-existant-filename
ls: cannot access 'non-existant-filename': No such file or directory

$ echo $_
non-existant-filename
  • First it returns nothing
  • Second it returns the last command used
  • Last it returns the last parameter used

This might be a handy variable for bash scripts but only if it's function is fully understood.


Some useful applications of _

I found some useful applications of _.

_ contains the last filename you can recycle

In this example _ is used to keep the last filename which you can reuse in subsequent commands without retying it.

$ ll ~/python/scroll1.py
-rwxrwxrwx 1 rick rick 2384 Dec 27 09:15 /home/rick/python/scroll1.py*

$ $_
# The python program ~/python/scroll1.py is executed

$ cat $_
#!/usr/bin/env python
# -*- coding: utf-8 -*-
   (... SNIP ... remaining contents of ~/python/scroll1.py appears on screen)
  • First command uses ll to list a python script filename. The filename is saved to _ for future use.
  • Second command $_ runs the python script.
  • Third command cat $_ lists the contents of the python script.

So the $_ variable/parameter can save some typing.

_ contains the last program run

Here's an example of differences between env and printenv updating the _ variable/parameter:

$ env > env.txt

$ printenv > printenv.txt

$ diff env.txt printenv.txt
66c66
< _=/usr/bin/env
---
> _=/usr/bin/printenv

Because a parameter wasn't passed to either command, the _ isn't updated with the last used parameter as in the previous example but, it is updated with the last command used.

Also noticed how _ is updated before the commands env and printenv are executed because _ it appears in the output.

4
  • Not a CS pro but this is a nice variable to use! Dec 27, 2019 at 15:04
  • 1
    It's not an env variable, it's a special Bash parameter. Basically an internal function/variable in Bash. See my answer
    – Thomas Ward
    Dec 27, 2019 at 15:10
  • 1
    @ThomasWard it kind of is an environment variable in the sense I discovered it using the commands env and printenv. But you cannot set it like a regular variable. For example using: _="Hello World"`` followed by: echo "$_"` doesn't display the new value. So as your answer states you cannot assign a value to it, the variable is automatically reset whenever you type a command. Dec 27, 2019 at 15:32
  • 2
    @WinEunuuchs2Unix because it's not a variable - it's a builtin parameter. Just like you can't assign a value to $? which is the exit code of the last process run.
    – Thomas Ward
    Dec 27, 2019 at 16:21

1 Answer 1

12

It's not an "Environment Variable". It's a special Bash parameter that is handled specially by Bash.

From the Bash Beginners Guide which explains this pretty well for Bash:

3.2.5. Special parameters

The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.

...

$_: The underscore variable is set at shell startup and contains the absolute file name of the shell or script being executed as passed in the argument list. Subsequently, it expands to the last argument to the previous command, after expansion. It is also set to the full pathname of each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file.

(Format of the quote adjusted for Ask Ubuntu, but contains all the information)

They also include a nice example of how $_ expands:

franky ~> grep dictionary /usr/share/dict/words
dictionary

franky ~> echo $_
/usr/share/dict/words
5
  • The _ parameter is explained in man bash/PARAMETERS/Special Parameters.
    – dessert
    Dec 28, 2019 at 7:06
  • 1
    Also note that the specs say "The underscore variable is ..." Dec 28, 2019 at 9:11
  • 1
    @HagenvonEitzen Also note it shows up in env and printenv output. There is more to this "special parameter" than meets the eye. Dec 28, 2019 at 23:56
  • @ThomasWard agreed it's a "special parameter" and not an "environment variable" and your answer is worthy of correcting that gaff. My gaff garners the two down-votes on my question I guess. Because _ shows up in env and printenv it was an honest mistake calling it an environment variable. I've upvoted your answer BTW. The spirit of the question is still valid regardless of special parameter / environment variable designation, How can I use $_? Dec 29, 2019 at 0:03
  • @WinEunuuchs2Unix ... which is why I provided the explanation of the parameter, and the examples they provided of a use of how it could be used. How you utilize the 'arguments' provided to a prior command is entirely dependent on your needs, I'm not sure there's a specific example that we can give you of how exactly to use it.
    – Thomas Ward
    Dec 29, 2019 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.