How-To: Bash
Documentation & References
Command Line Editing Cheat Sheet
- CTRL + c : Kill the foreground process.
- CTRL + z : Suspend the foreground process.
- CTRL + d : Exit the shell.
- CTRL + l : Clears the screen.
- CTRL + e : Go to the end of the line.
- CTRL + a : Go to the beginning of the line.
- CTRL + h : Backspace.
- CTRL + u : Clears the line before the cursor position.
- CTRL + k : Delete everything after the cursor, adding the killed text to the delete buffer.
- CTRL + w : Delete the word before the cursor, adding the killed text to the delete buffer.
- CTRL + y : Pastes the contents of the delete buffer.
- CTRL + _ : Undoes the last edit.
- ESC + l : Convert the word to lowercase letters.
- ESC + l : Convert the word to uppercase letters.
- ESC + f : Move forward a word.
- ESC + b : Move backward one word.
- ESC + 2 then ESC + b : Move back 2 words.
- CTRL + r : Interactively search backwards through the command history (repeat to search again).
- CTRL + s : Interactively search forward through the command history (repeat to search again).
- CTRL + g : Abort the interactive search and restore the command line.
- !! : Repeat the last command.
- !-2 : Repeat the previous to last command.
- !5 : Repeat command from history line 5 (run history to see the history).
- !echo : Repeat the last command that started with echo.
- !?date? : Repeat the last command that contains (not necessarily starts with) date.
- !:3 : Replaces with the third argument from the last command.
- !:1-2 : Replaces with the first through second arguments from the last command.
- !:^ : Replaces with the first argument from the last command.
- !:$ : Replaces with the last argument from the last command.
- !:$:h : Replaces with the last argument from the last command, removing the trailing pathname component.
- !:$:r : Replaces with the last argument from the last command, removing the trailing suffix/extension.
- !:$:e : Replaces with the last argument from the last command, removing all but the suffix/extension.
- !:* : Replaces with all arguments from the last command.
- ^old^new^ : Repeat the last command substituting old with new.
- !:s/old/new/ : Repeat the last command substituting old with new (add g to the end to replace all occurrences).
- ((x=y*5)) : Sets the variable x to the value of the variable y times 5.
Tips & Tricks
Disabling saving of the command history
This will disable saving the command history when you exit Bash:
Clearing the command history
This will clear the command history:
Iterating over a sequence of numbers
To iterate a loop across a series of numbers use 'seq'. The following will display 'line# X' with X starting at 4 and ending at 12 with a step size of 2. The step size is optional, with a default value of 1. The ending value is also optional, when it is omitted the single value denotes the ending value with an implicit starting value of 1.
for i in $(seq 4 2 12)
do
echo "line# $i"
done
Extended pattern matching with 'extglob'
If the extglob shell option is enabled using the shopt builtin (with
shopt -s extglob), several extended pattern matching operators are recognized. In the following description, a pattern-list is a list of one or more patterns separated by a |. Composite patterns may be formed using one or more of the following sub-patterns:
?(pattern-list)
Matches zero or one occurrence of the given patterns
*(pattern-list)
Matches zero or more occurrences of the given patterns
+(pattern-list)
Matches one or more occurrences of the given patterns
@(pattern-list)
Matches one of the given patterns
!(pattern-list)
Matches anything except one of the given patterns
The following example will list all files ending in avi or mkv:
In the following example only files that do not end in avi or mkv will be listed:
ShellCheck: Shell script analysis tool
ShellCheck is a lint style tool that catches common shell scripting mistakes.