Bash Operators
Process Management Operators
Symbol | Meaning | Example |
---|---|---|
& | Run in background | sleep 10 & |
&& | Run next only if previous succeeds | mkdir newdir && cd newdir |
; | Run multiple commands sequentially | echo "Hello"; echo "World" |
() | Run in a subshell (isolated environment) | (cd /tmp; ls) |
{} | Group commands in current shell | { cd /tmp; ls; } |
| | Pipe output of one command into another | `ls -l |
Variable & Expansion Operators
Symbol | Meaning | Example |
---|---|---|
$VAR | Expands variable | echo $HOME |
$$ | PID of current shell | echo $$ |
$! | PID of last background process | sleep 60 & echo $! |
$? | Exit status of last command | ls /bad && echo $? |
$0 | Script name | echo $0 |
$1 , $2 , ... | Positional arguments | ./script.sh arg1 → $1 = arg1 |
$# | Number of args passed | echo $# |
$@ | All args as separate strings | for arg in "$@"; do echo $arg; done |
$* | All args as one string | echo "$*" |
${VAR} | Explicit expansion | echo "Path: ${HOME}/bin" |
${VAR:-default} | Use default if unset | echo ${NAME:-Guest} |
Input / Output Redirection
Symbol | Meaning | Example |
---|---|---|
> | Redirect stdout (overwrite) | echo "Hi" > file.txt |
>> | Redirect stdout (append) | echo "More" >> file.txt |
< | Read from file | wc -l < file.txt |
2> | Redirect stderr | ls /bad 2> error.log |
&> | Redirect stdout + stderr | mycmd &> all.log |
`> | ` | Overwrite even with noclobber |
tee | Output to file and stdout | `echo "Log" |
String & Pattern Matching
Symbol | Meaning | Example |
---|---|---|
* | Match any number of characters | ls *.txt |
? | Match one character | ls file?.txt |
[...] | Match any in set | ls file[123].txt |
[^...] | Match any NOT in set | ls file[^123].txt |
{A,B} | Expand to A, B | echo {red,blue}.jpg |
{1..5} | Expand numeric sequence | echo file{1..5}.txt |
Command Substitution
Syntax | Meaning | Example |
---|---|---|
`command` | Replace with command output | echo `date` |
$(command) | Same, supports nesting | echo $(date) |
Quotes & Escaping
Symbol | Meaning | Example |
---|---|---|
'...' | Strong quote, no expansion | echo '$HOME' |
"..." | Weak quote, allows expansion | echo "$HOME" |
\ | Escape character | echo \"Hello\" |
\n | Newline (used with echo -e ) | echo -e "Hi\nThere" |
Special Built-in Commands
Symbol | Meaning | Example |
---|---|---|
: | No-op (does nothing) | : && echo "Continues" |
true | Always succeeds | true && echo "Success" |
false | Always fails | `false |
exit | Exit shell with status | exit 1 |
exec | Replace shell with command | exec ls |
source | Run script in current shell | source ~/.bashrc |
Loops & Conditionals
Syntax | Meaning | Example |
---|---|---|
if ... then ... fi | Conditional block | if [ -f file ]; then echo "Yes"; fi |
[ ... ] | Basic test | [ -d /home ] && echo "Exists" |
[[ ... ]] | Extended test | [[ "x" == "x" ]] && echo "Match" |
for ... in ... | Loop over list | for i in {1..3}; do echo $i; done |
while ... | Loop while condition | while true; do echo "Hi"; sleep 1; done |
Background & Job Control
Symbol | Meaning | Example |
---|---|---|
& | Run in background | sleep 10 & |
jobs | List background jobs | jobs |
fg | Resume job in foreground | fg %1 |
bg | Resume job in background | bg %1 |
nohup | Run after logout | nohup script.sh & |
disown | Remove from job table | disown %1 |
TL;DR: Essential Symbols
✔ Process control: &, &&, ||, ()
✔ Variables: $, $$, $!, $@
✔ Redirection: >, <, 2>, |
✔ Patterns: *, ?, {}
✔ Quoting: '...', "...", \
✔ Job control: jobs, fg, bg, disown