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/ 
[kevinkingsbury@host:~]$ echo $_
[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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.