Searched for tag shortcut and found 2 results in 0.5 ms

Bash command arguments

If you use Bash at the command-line you have probably found a need for some shortcut keys. If you have a long command like:

ls -la --size /some/long/path/to/file.txt

Often in a follow up command you will want to reference the previous command's arguments. In this example if you want to reference the file from the previous command you either have to type it all out again, copy and paste the previous argument, or use a Bash shortcut.

Shortcut Description
!! entire previous command
!$ last argument of previous command
!^ first argument of previous command
!:2 second argument
!:1-3 arguments 1 through 3
!* all arguments

You could use the !$ shortcut to reference the last parameter from the previous command

vim !$

This is a lot less typing, and quicker. The ^ and $ also line up nicely with the logic from regular expressions: first and last. Another real world example that I run into is creating a directory and then changing into it:

mkdir /var/tmp/user/path
cd !$
Tags:
Leave A Reply

Bash: Using previous command's parameters

Bash allows you to save some typing by referencing the previous command's parameters via a variable. If you want to reference the first parameter from the previous command you would use !^ and if you want the last parameter you would use !$. A real world example would be something like this:

mkdir /long/dir/structure/new_dir
cd !$

This would make a new directory and then change to that directory. Alternately you can reference numbered parameters using !:2 to reference the 2nd parameter.

Tags:
Leave A Reply