Share this post
FaceBook  Twitter  
Contents[Hide]

1. Introduction

Although you may think that you have learned to master Linux command line with bash shell, there are always some new tricks to learn to make your command line skills more efficient. This article will teach you a few more basic tricks on how to make your life with the Linux command line & bash more bearable and even enjoyable.

2. Bash Command History Expansion

This section will mostly deal with bash shortcuts in combination with three bash history expansion characters "!", "^" and "#". Bash Command History Expansion character "!" indicates start of history expansion. The "^" is a substitution character to modify a previously run command. The last optional character is "#", which denotes the reminder of the line as a comment.

2.1. Repeat last command

$ echo Bash Shortcuts
Bash Shortcuts
$ !!
echo Bash Shortcuts
Bash Shortcuts

!! is probably the easiest and most popular bash shortcut, which simply shows and executes your last entered command. 

2.2. Repeat most recent command

$ echo Bash Shortcuts
Bash Shortcuts
$ wc -m /etc/bash_completion
45 /etc/bash_completion
$ !echo
echo Bash Shortcuts
Bash Shortcuts

Entering "!" character followed by keyword will instruct shell to search for a most recent command which starts with keyword. Minor modification of the previous command can be done in order to only print the most recent command but not execute it. This can be quite handy if you are unsure what was your most recent command and do not want to execute it before you are able to confirm its meaning. To do that simply add :p at the end of your command followed by !! if you are happy to execute it:

$ echo Bash Shortcuts
Bash Shortcuts
$ wc -m /etc/bash_completion
45 /etc/bash_completion
$ !echo:p
echo Bash Shortcuts
$ !!
echo Bash Shortcuts
Bash Shortcuts

2.3. All arguments of the last command

$ echo Bash Shortcuts
Bash Shortcuts
$ touch !*
touch Bash Shortcuts
$ ls
Bash  Shortcuts

The !* shortcut will expand to all arguments used by the last command. In our example above we have used all previous arguments used by the echo command to create files using the touch command with the same arguments.

2.4. First argument of last command

$ echo Bash Shortcuts
Bash Shortcuts
$ touch !^
touch Bash
$ ls
Bash

Similarly as in the previous example, in this example we have used bash shortcut !^ to reuse only the first argument of the last command.

2.5. Last argument of last command

echo Bash Shortcuts
Bash Shortcuts
$ touch !$
touch Shortcuts
$ ls
Shortcuts

The same as in the previous example we can also reuse on the last argument supplied to the previously run command.

2.6. Quick command substitution

In the following example we are going to rerun the previous command but substitute the word "linux" with "bash".

$ echo linux command line linux command line
linux command line linux command line
$ ^linux^bash^
echo bash command line linux command line
bash command line linux command line

The example above have substituted the first occurrence of the first occurrence of the keyword Linux with bash. This is equivalent to:

$ !!:s/linux/bash/

2.7. Execute nth command from history

By default bash shell keeps a track of all commands you have executed previously as a history. Each command in your bash history has its relevant number. The following example will execute 189th command in your bash command history.

$ !189

Similarly as in previous examples you can, instead of executing the command directly, print it fist with :p.

 $ !189:p

HINT: To see last 5 commands in your bash command history execute: $ history 5

To execute last 4th command from your bash history use decrement -4:

$ !-4

2.8. Repeat entire command line

$ echo bash command line !#
echo bash command line echo bash command line
bash command line echo bash command line

The !# causes bash upon the command execution to repeat all what you have typed in the entire command line. You can limit this behavior by printing only certain keywords with  :nth keyword. For example, to re-print only second keyword you can use:

$ echo bash command line !#:2
echo bash command line command
bash command line command

3. Shortcuts to edit your bash command line

List of Basic Bash Command Line editing shortcuts

CTRL + f Move forward one word
CTRL + b Move backward one word
ALT + c Capitalize current character at the cursor and move to the end of the word
ALT + u Make all characters uppercase starting from the current cursor position to the end of the word
ALT + l Make all characters lowercase starting from the current cursor position to the end of the word
ALT + d Delete all characters starting from the current cursor position to the end of the word
ALT + f Move forward word by word
ALT + t Swap current word with previous
CTRL + t Swap current character with previous
CTRL + k Delete all from current cursor position to the end of the command line
CTRL + y Paste text or characters previously deleted with deletion shortcuts

4. Conclusion

The aim of this article was to introduce a few basic bash shortcuts used on a Linux command line. For further reading access manual page of bash and history:

$ man bash
$ man history

Partners

Who are we?

LinuxCareer.com is not affiliated with any local or international company, nor is it a recruitment or employment agency. We specialise in Linux based careers and closely related Information Technology fields by providing careers advice and latest employment opportunities.

JOIN LINUXCAREER

You can also get involved in the LinuxCareer project by participating on our FORUM or SUBMITTING A LINUX ARTICLE. We offer a range of privileges to our authors and good company.