Posted: . At: 4:57 PM. This was 5 years ago. Post ID: 4502
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.

Misc BASH shell tricks for the Linux user.

To recall the last entered command on the Linux console, press the Ctrl-P key shortcut. You may also press the up arrow to recall the command, but the Ctrl-P shortcut is another good way to perform this feat. The CTRL-A shortcut will jump to the beginning of the line you are currently typing and CTRL-E will jump to the end. Use the CTRL-L command to clear the screen, this is similar to the clear command. There are more shortcut examples for the BASH shell here: http://www.howtogeek.com/howto/ubuntu/keyboard-shortcuts-for-bash-command-shell-for-ubuntu-debian-suse-redhat-linux-etc/.

Superimposing the arguments of the last command into the next… Using the $_ argument. This is a command line argument that will contain the contents of the command line parameters last entered on the command line.

Just like this.

4.4 Tue Aug 27 jason@Yog-Sothoth 0: $ echo $_
-ahul
4.4 Tue Aug 27 jason@Yog-Sothoth 0: $ ls -ahul
total 4.7M
drwxrwxrwx+ 2 jason jason 4.0K Aug 26 09:09 .
drwxr-xrwx+ 3 jason jason 4.0K Aug 27 08:52 ..
-rwxrwxrwx  1 jason jason 140K Jul 17 11:44 megafon_music_10.ogg
-rwxrwxrwx  1 jason jason  49K Jul 17 11:44 megafon_music_11.ogg
-rwxrwxrwx  1 jason jason 688K Jul 17 11:44 megafon_music_12.ogg
-rwxrwxrwx  1 jason jason 1.2M Aug 10 09:26 megafon_music_13.ogg
-rwxrwxrwx  1 jason jason 156K Jul 17 11:44 megafon_music_14.ogg
-rwxrwxrwx  1 jason jason 346K Jul 17 11:44 megafon_music_15.ogg
-rwxrwxrwx  1 jason jason 831K Aug 10 09:24 megafon_music_1.ogg
-rwxrwxrwx  1 jason jason 277K Aug 10 09:24 megafon_music_2.ogg
-rwxrwxrwx  1 jason jason 316K Aug 10 09:24 megafon_music_3.ogg
-rwxrwxrwx  1 jason jason 392K Aug 10 09:24 megafon_music_4.ogg
-rwxrwxrwx  1 jason jason  27K Jul 17 11:44 megafon_music_5.ogg
-rwxrwxrwx  1 jason jason  48K Jul 17 11:44 megafon_music_6.ogg
-rwxrwxrwx  1 jason jason  88K Jul 17 11:44 megafon_music_7.ogg
-rwxrwxrwx  1 jason jason  84K Jul 17 11:44 megafon_music_8.ogg
-rwxrwxrwx  1 jason jason 150K Jul 17 11:44 megafon_music_9.ogg

Then running the ls command with the previous arguments.

4.4 Tue Aug 27 jason@Yog-Sothoth 0: $ ls $_
total 4.7M
drwxrwxrwx+ 2 jason jason 4.0K Aug 26 09:09 .
drwxr-xrwx+ 3 jason jason 4.0K Aug 27 08:52 ..
-rwxrwxrwx  1 jason jason 140K Jul 17 11:44 megafon_music_10.ogg
-rwxrwxrwx  1 jason jason  49K Jul 17 11:44 megafon_music_11.ogg
-rwxrwxrwx  1 jason jason 688K Jul 17 11:44 megafon_music_12.ogg
-rwxrwxrwx  1 jason jason 1.2M Aug 10 09:26 megafon_music_13.ogg
-rwxrwxrwx  1 jason jason 156K Jul 17 11:44 megafon_music_14.ogg
-rwxrwxrwx  1 jason jason 346K Jul 17 11:44 megafon_music_15.ogg
-rwxrwxrwx  1 jason jason 831K Aug 10 09:24 megafon_music_1.ogg
-rwxrwxrwx  1 jason jason 277K Aug 10 09:24 megafon_music_2.ogg
-rwxrwxrwx  1 jason jason 316K Aug 10 09:24 megafon_music_3.ogg
-rwxrwxrwx  1 jason jason 392K Aug 10 09:24 megafon_music_4.ogg
-rwxrwxrwx  1 jason jason  27K Jul 17 11:44 megafon_music_5.ogg
-rwxrwxrwx  1 jason jason  48K Jul 17 11:44 megafon_music_6.ogg
-rwxrwxrwx  1 jason jason  88K Jul 17 11:44 megafon_music_7.ogg
-rwxrwxrwx  1 jason jason  84K Jul 17 11:44 megafon_music_8.ogg
-rwxrwxrwx  1 jason jason 150K Jul 17 11:44 megafon_music_9.ogg

This is how to rerun the previous command in your command history. The !! operator returns the last run command in your history. And the $_ operator returns the last parameters.

4.4 Tue Aug 27 jason@Yog-Sothoth 0: $ !! $_
ls -hula $_
total 4.7M
drwxrwxrwx+ 2 jason jason 4.0K Aug 26 09:09 .
drwxr-xrwx+ 3 jason jason 4.0K Aug 27 08:52 ..
-rwxrwxrwx  1 jason jason 140K Jul 17 11:44 megafon_music_10.ogg
-rwxrwxrwx  1 jason jason  49K Jul 17 11:44 megafon_music_11.ogg
-rwxrwxrwx  1 jason jason 688K Jul 17 11:44 megafon_music_12.ogg
-rwxrwxrwx  1 jason jason 1.2M Aug 10 09:26 megafon_music_13.ogg
-rwxrwxrwx  1 jason jason 156K Jul 17 11:44 megafon_music_14.ogg
-rwxrwxrwx  1 jason jason 346K Jul 17 11:44 megafon_music_15.ogg
-rwxrwxrwx  1 jason jason 831K Aug 10 09:24 megafon_music_1.ogg
-rwxrwxrwx  1 jason jason 277K Aug 10 09:24 megafon_music_2.ogg
-rwxrwxrwx  1 jason jason 316K Aug 10 09:24 megafon_music_3.ogg
-rwxrwxrwx  1 jason jason 392K Aug 10 09:24 megafon_music_4.ogg
-rwxrwxrwx  1 jason jason  27K Jul 17 11:44 megafon_music_5.ogg
-rwxrwxrwx  1 jason jason  48K Jul 17 11:44 megafon_music_6.ogg
-rwxrwxrwx  1 jason jason  88K Jul 17 11:44 megafon_music_7.ogg
-rwxrwxrwx  1 jason jason  84K Jul 17 11:44 megafon_music_8.ogg
-rwxrwxrwx  1 jason jason 150K Jul 17 11:44 megafon_music_9.ogg

Running the sudo !! command will rerun the last command in your history with sudo, like in the example below.

4.4 Tue Aug 27 jason@Yog-Sothoth 0: $ cat /etc/shadow
cat: /etc/shadow: Permission denied
4.4 Tue Aug 27 jason@Yog-Sothoth 0: $ sudo !!

This is a very useful shortcut if the command-line arguments were very long and you do not wish to retype them again. I found this useful tip here: http://stefaanlippens.net/node/54. If you append an & character to the end of a command in the terminal exempli gratia the xterm command to load another terminal, it will relinquish control of your terminal and you will still be able to type in the original terminal as well as the new one. For example you would type xterm & and the xterm will load from the existing terminal and give it back to you. The CTRL-R key shortcut will allow you to search the command history for a certain command and execute it. Below is what the command search prompt looks like.

(reverse-i-search)`l': ls -ahul

Leave a Comment

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