guest@linuxthings:~/guides$ man shell-features
Shell Features
The bash shell provides powerful features to speed up your workflow and automate tasks.
Wildcards (Globbing)
Pattern | Matches |
|---|---|
* | Zero or more characters |
? | Exactly one character |
[abc] | Any single character in the set (a, b, or c) |
[a-z] | Any single character in the range |
[!abc] or [^abc] | Any character NOT in the set |
ls *.jpg # List all JPEG files
ls file?.txt # List file1.txt, fileA.txt, etc.
rm [A-Z]* # Remove files starting with a capital letter
ls .bash* # Dot files must be matched with a literal leading dotThe shell, not the program, expands wildcards: ls only ever sees the final list of filenames. Patterns never match a leading dot or a /, which is why hidden "dot files" stay out of * results. To match a literal - inside brackets, put it first or last, as in [a-z-].
Brace Expansion
Braces generate strings whether or not matching files exist, unlike [...] patterns, which only match existing filenames.
echo file{1,2,3}.txt
# Output: file1.txt file2.txt file3.txt
echo {1..5} # 1 2 3 4 5
echo {A..E} # A B C D E
mkdir -p project/{src,docs,tests}
mv image.{jpeg,jpg}
# Expands to: mv image.jpeg image.jpg (renames the file)Command History
- Up Arrow: Recall previous commands.
- history: List entire command history.
- history 20: Show only the last 20 commands.
- !!: Re-run the very last command.
- !N: Re-run command number N from the history list.
- !string: Re-run the last command starting with 'string'.
- !$: The last argument of the previous command.
- !*: All arguments of the previous command.
- Ctrl+R: Interactive reverse search through history.
ls z* # Check what the pattern matches first...
rm !$ # ...then reuse it: same as rm z*Quoting and Escaping
echo "Home is $HOME" # Double quotes: variables are expanded
echo 'Home is $HOME' # Single quotes: everything is literal
echo a\* # Backslash escapes one special character
ls -l "My Document.txt" # Quote filenames that contain spacesI/O Redirection
cmd > file # Redirect output to file (overwrite)
cmd >> file # Append output to file
cmd < file # Read input from file
cmd 2> file # Redirect error messages to file
cmd > out 2> err # Output and errors to separate files
cmd &> file # Output and errors to the same file
cmd 2>/dev/null # Discard error messages
cmd1 | cmd2 # Pipe output of cmd1 to input of cmd2
cmd | tee file # Show output and save a copy to fileCommand and Process Substitution
echo "Today is $(date +%A)" # Insert a command's output
echo "Next year: $(( $(date +%Y) + 1 ))"
diff <(ls dir1) <(ls dir2) # Treat command output as a file$(...) is preferred over the older backquote form because it can be nested.
Tab Completion
Press Tab while typing a command or filename and bash completes it. If several names match, press Tab twice to list the candidates, then type a few more characters and press Tab again.
Shell Startup Files
File | When bash reads it |
|---|---|
~/.bash_profile (or ~/.profile) | Login shells, e.g. SSH sessions and console logins |
~/.bashrc | Every new interactive shell |
~/.bash_logout | When a login shell exits |
/etc/profile, /etc/bash.bashrc | System-wide equivalents |
Put aliases and prompt settings in ~/.bashrc. Apply changes to the current shell without logging out by running source ~/.bashrc.
Multiple Shells with tmux
tmux runs several shells inside one terminal and keeps them alive if your SSH connection drops. Every command starts with the prefix key Ctrl+B.
Keys | Action |
|---|---|
Ctrl+B c | New window |
Ctrl+B n / p | Next / previous window |
Ctrl+B % | Split into side-by-side panes |
Ctrl+B " | Split into top and bottom panes |
Ctrl+B o | Jump to the next pane |
Ctrl+B d | Detach (reattach later with |
Ctrl+B ? | Show all key bindings |