Arithmetic Operators |
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulo |
** |
Exponentiation |
Attribution Operators |
= |
Attribute some value to a variable |
+= |
Increase a variable with a constant |
-= |
Decrease a variable with a constant |
*= |
Multiply the variable by a constant |
/= |
Divide a variable by a constant |
%= |
Remainder of division by a constant |
++ |
Increase 1 to the variable value |
-- |
Decrease 1 from the variable value |
Comparison Operators |
== |
Equals |
!= |
Not equals to (different) |
> |
Greater than |
>= |
Greater than or equal to |
< |
Less than |
<= |
Less than or equal to |
Logical Operators |
&& |
Logical AND |
|| |
Logical OR |
Bitwise Operators |
<< |
Left-shift |
>> |
Right-shift |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise Exclusive OR (XOR) |
~ |
Bitwise NOT (complement) |
! |
NOT |
Bitwise Operators (attribution) |
<<= |
Left-shift |
>>= |
Right-shift |
&= |
Bitwise AND |
|= |
Bitwise OR |
^= |
Bitwise Exclusive OR (XOR) |
Operator |
Action |
< |
Redirect the standard input (STDIN) |
> |
Redirect the standard output (STDOUT) |
2> |
Redirect the error output (STDERR) |
>> |
Redirect the standard output, appending |
2>> |
Redirect the error output, appending |
| |
Connects the standard output with standard input of another command |
2>&1 |
Connects the error output with standard output |
>&2 |
Connects the standard output with error output |
>&- |
Closes the standard output |
2>&- |
Closes the error output |
3<>file |
Connects the file descriptor 3 to file 'file' |
<<FIM |
Feeds the standard input (Here Document) |
<<-FIM |
Feeds the standard input, striping TABs |
<(cmd) |
The output of command 'cmd' is a file: diff <(cmd1) <(cmd2) |
>(cmd) |
The input of command 'cmd' is a file: tar cf >(bzip2 -c >file.tbz) $dir |
Variable |
Positioning parameters |
$0 |
Parameter #0 (name of command or function) |
$1 |
Parameter #1 (from command line or function) |
... |
Parameter #N ... |
$9 |
Parameter #9 (from command line or function) |
${10} |
Parameter #10 (from command line or function) |
... |
Parameter #NN ... |
$# |
Total number of parameters from command line or function |
$* |
All the parameters, in a single string |
$@ |
All the parameters, in many protected strings |
Variable |
Misc |
$$ |
PID of current process (or script) |
$! |
PID of last job in background |
$_ |
Last argument of last executed command |
$? |
Error code of last executed command |
Syntax |
Conditional Expansion |
${var:-text} |
If var is undefined, returns 'text' |
${var:=text} |
If var is undefined, set as 'text' |
${var:?text} |
If var is undefined, returns error message 'text' |
${var:+text} |
If var is defined, returns 'text', else return empty |
Syntax |
String expansion |
${var} |
Is the same of $var, but not ambiguous |
${#var} |
Return the string length |
${!var} |
Executes the content of $var (like 'eval \$$var') |
${!text*} |
Return the names of variables starting with 'text' |
${var:N} |
Return the text from position 'N' |
${var:N:len} |
Return 'len' chars from position 'N' |
${var#text} |
Cut 'text' from beginning of string |
${var##text} |
Cut 'text' from beginning of string (*greedy) |
${var%text} |
Cut 'text' from end of string |
${var%%text} |
Cut 'text' from end of string (*greedy) |
${var/text/new} |
Replace 'text' by 'new', once |
${var//text/new} |
Replace 'text' by 'new', all |
${var/#text/new} |
If string starts with 'text', replace 'text' by 'new' |
${var/%text/new} |
If string ends with 'text', replace 'text' by 'new' |
${var^} |
Uppercase the first character |
${var^^} |
Uppercase all the characters |
${var,} |
Lowercase the first character |
${var,,} |
Lowercase all the characters |
${var~} |
Reverse the case of the first character |
${var~~} |
Reverse the case of all the characters |
Syntax |
Description |
Example |
"..." |
Protects a string, but parse $, \ and ` as special chars |
"abc" |
'...' |
Protects a string completely (no special chars recognized) |
'abc' |
$'...' |
Protects a string completely, but interprets \n, \t, \a, etc |
$'abc\n' |
`...` |
Execute commands in a subshell, returning the result |
`ls` |
{...} |
Group commands |
{ ls ; } |
(...) |
Execute commands in a subshell |
( ls ) |
$(...) |
Execute commands in a subshell, returning the result |
$( ls ) |
((...)) |
Test an arithmetic operation, returning 0 or 1 |
((5 > 3)) |
$((...)) |
Return the result of a arithmetic operation |
$((5+3)) |
[...] |
Test an arithmetic expression, returning 0 or 1 (alias of command 'test') |
[ 5 -gt 3 ] |
[[...]] |
Test an expression, returning 0 or 1 (&& and || are allowed) |
[[ 5 > 3 ]] |
Escape |
Reminder |
Expands to... |
\a |
Alert |
Alert (beep) |
\d |
Date |
Date in format "Week-day Month Day" (Sat Jan 15) |
\e |
Escape |
Esc character |
\h |
Hostname |
Hostname without the domain (dhcp11) |
\H |
Hostname |
Full hostname (dhcp11.company) |
\j |
Jobs |
Number of active jobs |
\l |
Tty |
Name of current terminal (ttyp1) |
\n |
Newline |
New Line |
\r |
Return |
Carriage return |
\s |
Shell |
Shell name (basename $0) |
\t |
Time |
Time in 24 hours format HH:MM:SS |
\T |
Time |
Time in 12 hours format HH:MM:SS |
\@ |
At |
Time in 12 hours format HH:MM am/pm |
\A |
At |
Time in 24 hours format HH:MM |
\u |
User |
Current user login |
\v |
Version |
Bash Version (2.00) |
\V |
Version |
Bash Version + Subversion (2.00.0) |
\w |
Working Dir |
Full working dir ($PWD) |
\W |
Working Dir |
Last working dir (basename $PWD) |
\! |
History |
Number of current command in history |
\# |
Number |
Number of current command |
\$ |
ID |
Show "#" if user is root, and "$" in case of a regular user |
\nnn |
Octal |
Char which Octal value is nnn |
\\ |
Backslash |
Backslash \ literal |
\[ |
Escapes |
Start a escape sequence (color codes) |
\] |
Escapes |
Finish a escape sequence |
Letter |
Reminder |
File types (First char) |
- |
- |
Regular file |
d |
Directory |
Directory |
l |
Link |
Symbolic link |
b |
Block |
Block Device (Hard drives) |
c |
Char |
Character device (serial modem) |
s |
Socket |
Mapped socket in a file (processes communication) |
p |
Pipe |
FIFO or Named Pipe (processes communication) |
Letter |
Reminder |
File permission (next nine chars) |
- |
- |
Deactivated |
r |
Read |
Read Permission |
w |
Write |
Write permission |
x |
eXecute |
Execution permission (or directory access) |
X |
eXecute |
Directory access only |
s |
Set ID |
User/group for execution (SUID, SGID) - permission 'x' activated |
S |
Set ID |
User/group for execution (SUID, SGID) - permission 'x' deactivated |
t |
sTicky |
Users delete his own files only - permission 'x' activated |
T |
sTicky |
Users delete his own files only - permission 'x' deactivated |
Meta |
Name |
Description |
. |
Dot |
Matches exactly one char (any char) |
[] |
List |
Matches any char in the group |
[^] |
Negated list |
Matches any char, except those in the group |
? |
Optional |
Matches the preceding item zero or one time |
* |
Star |
Matches the preceding item zero or more times |
+ |
Plus |
Matches the preceding item one or more times |
{,} |
Repeat |
Matches the preceding item between m and n times {m,n} |
^ |
Caret |
Matches the start of the line |
$ |
Dollar |
Matches the end of the line |
\b |
Boundary |
Matches the word boundary |
\ |
Escape |
Escapes a metacharacter, taking off its power |
| |
OR |
Alternatives (use inside a group) |
() |
Group |
Group slices of expression, nesting is allowed |
\1 |
Backreference |
Returns group #1 contents |
\2 |
Backreference |
Returns group #2 contents (until \9) |
.* |
Catchall |
Matches everything, even nothing |
?? |
Lazy Optional |
Like regular Optional, but not greedy |
*? |
Lazy Star |
Like regular Star, but not greedy |
+? |
Lazy Plus |
Like regular Plus, but not greedy |
{}? |
Lazy Repeat |
Like regular Repeat, but not greedy |
Conditions with IF |
if [ -f "$file" ]; then echo 'File found'; fi |
if [ ! -d "$dir" ]; then echo 'Directory not found'; fi |
if [ $i -gt 5 ]; then echo 'Greater than 5'; else echo 'Lesser than 5'; fi |
if [ $i -ge 5 -a $i -le 10 ]; then echo 'Between 5 and 10, inclusive'; fi |
if [ $i -eq 5 ]; then echo '=5'; elif [ $i -gt 5 ]; then echo '>5'; else echo '<5'; fi |
if [ "$USER" = 'root' ]; then echo 'Hello root'; fi |
if grep -qs 'root' /etc/passwd; then echo 'User found'; fi |
Conditions with AND (&&) and OR (||) |
[ -f "$file" ] && echo 'File found' |
[ -d "$dir" ] || echo 'Directory not found' |
grep -qs 'root' /etc/passwd && echo 'User found' |
cd "$dir" && rm "$file" && touch "$file" && echo 'done!' |
[ "$1" ] && param=$1 || param='default value' |
[ "$1" ] && param=${1:-default value} |
[ "$1" ] || { echo "Usage: $0 parameter" ; exit 1 ; } |
Adds 1 to variable $i |
i=$(expr $i + 1) |
i=$((i+1)) |
let i=i+1 |
let i+=1 |
let i++ |
Loop from 1 to 10 |
for i in 1 2 3 4 5 6 7 8 9 10; do echo $i; done |
for i in $(seq 10); do echo $i; done |
for ((i=1;i<=10;i++)); do echo $i; done |
i=1 ; while [ $i -le 10 ]; do echo $i ; i=$((i+1)) ; done |
i=1 ; until [ $i -gt 10 ]; do echo $i ; i=$((i+1)) ; done |
Loop reading lines from a file or command output |
cat /etc/passwd | while read LINE; do echo "$LINE"; done |
grep 'root' /etc/passwd | while read LINE; do echo "$LINE"; done |
while read LINE; do echo "$LINE"; done < /etc/passwd |
while read LINE; do echo "$LINE"; done < <(grep 'root' /etc/passwd) |
Wildcards for case command |
case "$dir" in /home/*) echo 'dir inside /home';; esac |
case "$user" in root|john|mary) echo "Hello $user";; *) echo "I don't know you.";; esac |
case "$var" in ?) echo '1 letter';; ??) echo '2 letters';; ??*) echo 'More than 2 letters';; esac |
case "$i" in [0-9]) echo '1 digit';; [0-9][0-9]) echo '2 digits';; esac |
Dialog boxes |
dialog --calendar 'abc' 0 0 31 12 1999 |
dialog --checklist 'abc' 0 0 0 item1 'desc1' on item2 'desc2' off |
dialog --fselect /tmp 0 0 |
(echo 50; sleep 2; echo 100) | dialog --gauge 'abc' 8 40 0 |
dialog --infobox 'abc' 0 0 |
dialog --inputbox 'abc' 0 0 |
dialog --passwordbox 'abc' 0 0 |
dialog --menu 'abc' 0 0 0 item1 'desc1' item2 'desc2' |
dialog --msgbox 'abc' 8 40 |
dialog --radiolist 'abc' 0 0 0 item1 'desc1' on item2 'desc2' off |
dialog --tailbox /tmp/file.txt 0 0 |
dialog --textbox /tmp/file.txt 0 0 |
dialog --timebox 'abc' 0 0 23 59 00 |
dialog --yesno 'abc' 0 0 |
Hint 1: Dialog ... && echo 'You pressed OK/Yes' || echo 'You pressed Cancel/No' |
Hint 2: result=$(Dialog --stdout --BOXTYPE 'abc' ...) |
Shortcut |
Description |
Similar key |
Ctrl+A |
Move the cursor to the beginning of line |
Home |
Ctrl+B |
Move the cursor one position left |
← |
Ctrl+C |
Send EOF() to the system |
|
Ctrl+D |
Delete the char at right |
Delete |
Ctrl+E |
Move the cursor to the end of line |
End |
Ctrl+F |
Move the cursor one position at right |
→ |
Ctrl+H |
Delete the char at left |
Backspace |
Ctrl+I |
Complete files and commands |
Tab |
Ctrl+J |
Line break |
Enter |
Ctrl+K |
Cut from cursor to the end of line |
|
Ctrl+L |
Clear screen |
|
Ctrl+N |
Next command |
|
Ctrl+P |
Previous command |
|
Ctrl+Q |
Unlock the shell (see Ctrl+S) |
|
Ctrl+R |
Seek in the command history |
|
Ctrl+S |
Lock the shell (see Ctrl+Q) |
|
Ctrl+T |
Exchange two chars position |
|
Ctrl+U |
Cut the whole line |
|
Ctrl+V |
Insert a literal character |
|
Ctrl+W |
Cut word at left |
|
Ctrl+X |
Move the cursor to end/beginning of line (2x) |
Home/End |
Ctrl+Y |
Paste the text |
|
Command |
Function |
Useful options |
cat |
Show file contents |
-n, -s |
cut |
Extract fields |
-d -f, -c |
date |
Show date |
-d, +'...' |
diff |
Compare files |
-u, -Nr, -i, -w |
echo |
Show text |
-e, -n |
find |
Find files |
-name, -iname, -type f, -exec, -or |
fmt |
Format paragraph |
-w, -u |
grep |
Search for text |
-i, -v, -r, -qs, -n, -l, -w -x, -A -B -C |
head |
Show first lines |
-n, -c |
od |
Show chars |
-a, -c, -o, -x |
paste |
Merge files |
-d, -s |
printf |
Show text |
nothing |
rev |
Reverse text |
nothing |
sed |
Edit text |
-n, -f, s/this/that/, p, d, q, N |
seq |
Count numbers |
-s, -f |
sort |
Sort text |
-n, -f, -r, -k -t, -o |
tac |
Reverse file |
nothing |
tail |
Show last lines |
-n, -c, -f |
tee |
Save the stream |
-a |
tr |
Transforms the text |
-d, -s, A-Z a-z |
uniq |
Remove duplicated |
-i, -d, -u |
wc |
Count letters |
-c, -w, -l, -L |
xargs |
Manage arguments |
-n, -i |