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.

Subscribe to my spam free newsletter for other Laravel updates like this

I never spam, and only email when I have a good in-depth post published on my site (mostly about Laravel). You can also follow me on social media to get updates.

webdevetc profile pic
webdevetc

I'm a late 20's web developer, specialising in Laravel, but work with other PHP frameworks and associated technologies (such as JS). I mostly write about PHP here. Contact me here. Need to hire or work with a contract PHP software developer in London, UK (or freelance)? Contact me and check my availability.

Leave a Comment