Linux Tips I Wish I Knew Long Ago: $_

This is one I constantly forget, but $_ in a bash shell (and other languages like Perl and I know it’s in Windows Powershell but may have a different usage) is a special parameter, it gives the last argument to the previous command. This comes in handy all the time when editing and executing on the same thing. For example, here I’m working with a python script:

[kevinkingsbury@host:~]$ python ~/code/python/python-example-file/longass-filename-and-path.py 
[kevinkingsbury@host:~]$ echo $_
/home/kevinkingsbury//code/python/python-example-file/longass-filename-and-path.py
[07:46][kevinkingsbury@host:~]$ vi $_

You see that the $_ gets set to the file, so I can preform different commands on that file just by passing the variable, comes in handy with long paths.

Leave a Reply

Your email address will not be published. Required fields are marked *