Skip to main content
  1. Posts/

If I Had Known This Sooner: $_

·207 words·1 min·
ELI5 Bash

There are always times that I learned something useful and/or cool which I wish I had known sooner. So, I decided to write these little things down. Maybe these kind of things have the protential to be a series.

For anyone who constantly works on *nix systems, it’s really common often to interact with terminal.

One of the common tasks people do in terminal is creating a folder:

~ $ mkdir random_folder

That’s normal, right? But what if I want to go to that folder?

~ $ cd random_folder

It’s all good, man. But what if the path is long or has multiple levels?

~ $ mkdir -p random_folder/1/2/3/4/5/6/7/

Without the help of mouse support or smashing tab key, it’s really painful to change directory fast.

And of course, this is where $_ comes to rescue.

$_ is a bash parameter which tells you what the last argument is. So:

~ $ mkdir -p random_folder/1/2/3/4/5/6/7/
~ $ cd $_
~/random_folder/1/2/3/4/5/6/7 $

But how do we know if it really is the last argument?

~ $ echo 1 2
1 2
~ $ echo $_
2

References:

W.T. Chang
Author
W.T. Chang