Bash Parameter Expansions (!$, etc)

Bash Parameter Expansions (!$, etc) Thumbnail image

Bash is used everywhere. I prefer to only use bash, with minimal bashrc changes so when using it on a different server I have the same setup.

Here are some useful expansions that you can use in Bash. They're quite common, you probably know them already, and this won't be a massive post.

!$ is the last parameter of the previous command

Use !$ to get the last param of the most recent command.

For example if you did cp /home/file.txt /new_dir/file_new_location.txt, then you could delete the file you just created (file_new_location.txt) by doing rm !$. This is a huge time saver.

!* are all of the parameters of the previous command

Use !* for all of the params from the previous command.

For example, if you did echo file1 file2, you could then do cp !* to copy file1 to file2.

!!:n is the nth param of the previous command

And if you want a specific param (n), then use !!:n, where n is the number (1 = the first param).

For example if you did echo param1 param2 param3 param4, then did echo !!:3 in bash, it would echo param3.

webdevetc profile pic

I am a 29 year old freelance backend web developer from London, mostly focusing on PHP and Laravel lately. This is my site - I mostly write about PHP here. Contact me here (especially for any contracting/freelance PHP jobs early 2019 in London ;) ).

Leave a Comment