Skip to content
This repository has been archived by the owner on May 26, 2024. It is now read-only.

Latest commit

 

History

History
322 lines (308 loc) · 5.5 KB

bash.md

File metadata and controls

322 lines (308 loc) · 5.5 KB

Bash Cheatsheet

Special Arguments

Argument Explanation
$# Number of arguments
$* All arguments
$@ All arguments, starting from first
$1 First argument
$_ Last argument of previous command

See: Bash Hackers Wiki

Special Characters

Character Explanation
" " Whitespace
$var, ${var} Variable expansion
$(cmd) Command substitution, returns command output
$((expression)) Arithmetic expansion
'' Literal string, special chars are ignored
"" String, special chars are evaluated
\ Escape special chars
# Comment
= Variable Assignment
[[ ]] Evaluate conditional expression
! Negate test or exit status
>, >>, < Redirection
| Pipe output to another command
; Command separator
{ } Inline group, commands inside are treated as one command
( ) Subshell group, commands are executed in a new process
(( )) Arithmetic expression, returns result
*, ? Globs, wildcard matches
~ Home directory
& Background command

See: Bash Guide

Loops

Loop Multi-Line One-Liner
For
for i in list; do
    cmd
done
            
for i in list; do cmd; done
            
If-Else
if [ condition ]; do
    cmd
elif [condition ]; do
    cmd
else cmd
fi
            
if [ condition ]; do cmd; else cmd; fi
            
While
while [ condition ]; do
    cmd
done
            
while [ condition ]; do cmd; done
            

Text Manipulation

Command Explanation Usage
Sed Stream Editor - transform text
sed -i "s/to replace/replacement/g"
            
Cut Cut fields using delimiter
cut -d "<delimiter>" -f "<field # to cut>"
            

Colorization/Formatting

See: Bash tips: Colors and formatting

Resources