Bash Cheatsheet
Bash Cheatsheet
io/vMQlp
Bash, a command line interface for interacting with 1.4.2 Parameter expansion 1.6 Control flow statements
the operating system, was created in the 1980s. Other
1. ${var^}, ${var,} convert first character to The one-line constructs && and || work not like and,
popular shells are zsh and fish.
upper and lowercase. ${var^^}, ${var,,} or (, ), but the if then else statement.
do the same to all characters.
1 Programming in Bash 1.6.1 Conditionals
${var~}, ${var~~} are undocumented now:
1.1 Shebang they reverse the case. Here at least one statement must be specified inside
every block, but one can use a single colon (:) as a null
The shebang (#!) at the head of a script indicates an In case of the array expansion, every expanded
statement to avoid rewriting the code.
interpreter for execution, as in #!/bin/bash. Lines element changes case, no matter what.
starting with a # (with the exception of shebang) are if condition; then
2. ${var#pattern} removes the pattern from
comments and thus wont be executed. commands
the beginning of the string, if possible.
elif second_condition; then
1.2 Quoting and literals Its greedy variant is ${var##pattern}. some_commands
else
Single quotes preserve the literal value of charac- ${var%pattern} and ${var%%pattern} do
other_commands
ters enclosed within them. A single quote may not the same, but from the end of the string.
fi
appear between single quotes, even when escaped, Application: extracting parts of a filename.
but may appear between double quotes "". select word in "Bash" "Haskell" "Python"
3. ${var/pattern/string} performs a single
They work similarly, with an exception that the shell do
search and replace operation.
expands any variables that appear within them. echo "Your language is $word".
${var//pattern/string} searches for all done
1.3 Variables occurrences of the pattern and replaces them.
There is also a case instruction:
4. ${#var} returns length of the string.
Variable names are case sensitive. They can contain case $language in
digits and underscores as well, but a name starting 5. ${var:offset:length} skips first offset bash)
with a digit is not allowed. Example: characters from var and truncates the output echo "Bourne Again Shell!"
var="kind" to given length. :length may be skipped. ;;
echo ${var}ness # kindness python|haskell)
Negative values separated with extra space are
echo "Python or Haskell!"
accepted.
Special variables: exit 1
1. $0: name of the script itself. 6. ${var:-value} uses a default value, if var is ;;
2. $1, $2, $3, . . . : the first, second, etc. argument. empty or unset. *)
shift removes first argument and advances echo "Unknown language!"
${var:=value} does the same, but performs
rest of them forward. ;; # optional
an assignment as well.
3. $* and $@ denote all the positional parameters. esac
4. $#: the number of positional parameters ${var:+value} uses an alternative value if
5. $?: exit status of last executed command. var isnt empty or unset! 1.6.2 Testing conditions
6. $$: the process ID of the shell. Remember that test command follows symbolic
7. $!: the process ID of last executed command. 1.4.3 Command substitution
links (except for the -h test).
To read a line of input, use read shell built-in. To execute commands in a subshell and then pass
1. File tests:
their standard output, use $( commands ).
read reads a line from the stdin: (a) -e file exists, -s file is nonempty,
n returns after reading n characters, (b) -d directory, -f regular file, -h symlink,
1.4.4 Arithmetic expansion
n displays a prompt. (c) -b block device, -c character device,
The arithmetic expression $(( ... )) is evaluated (d) -p named pipe, -S socket.
and expands to the result. Bash guarantees that the 2. File permissions:
1.4 Expansions output will be a one-word integer. (a) -r readable, -w writable, -x executable,
After the command has been split into tokens, these (b) -u setuid, -g setgid, -k sticky bit.
tokens or words are expanded or resolved. There are 1.4.5 Process substitution 3. String tests: -z empty, -n nonempty.
eight kinds of expansion performed, which we will 4. Arithmetic tests:
This kind of substitution (where input or output of a
discuss in the next sections, in the order that they are (a) -eq =, -ne 6=,
command appears as a temporary file) is performed
expanded. (b) -lt <, -gt >,
simultaneously with the following: arithmetic and
(c) -le , -ge .
parameter expansions, command substitution.
1.4.1 Brace expansion
<( ... ) # not specified by POSIX! 1.6.3 Loops
Brace expansion is used when we need to generate all
>( ... )
possible string combinations. Both of the commands for var in "the first" "the second"; do
produce the same output: echo "${var}"
1.5 Streams done
echo {I,really,love,dots}.
echo I. really. love. dots. There are always three default files open:
for (( i = 1; i <= 10; i++ )); do
Warning: it does not expand the variables ($var), 1. stdin (the keyboard, file descriptor 0), echo "i = ${i}."
which is done later, but supports ranges (sequences) 2. stdout (the screen, file descriptor 1) and done # C-style
of characters: 3. stderr (error messages output, file descriptor
2). while read myline; do
echo {a..t}
These streams can be redirected: echo "It says ${myline}"
a b c d e f g h i j k l m n o p q r s t
done < some_file
and (maybe zero paded or with an increment rate) 1. cmd > file redirects to a file (overwrites),
integers, assuming the Bash version is 4 or newer: 2. cmd >> file appends instead, As Bash Guide for Beginners by M. Garrels says:
3. m>n (or m>&n) redirects a file descriptor to a file 1. the break statement is used to exit the current
echo {01..10..1}.~ (or another file descriptor), loop before its normal ending.
01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 4. &>file redirects stdout and stderr to a file, 2. the continue statement resumes iteration of
There is a tilde expansion as well. The expressions 5. :> file truncates file to zero length, an enclosing while, until, select or for loop.
and <user> expand to the home directory of the 6. | (pipe) serves as a command chaining tool.
current (or given) user.
2 Emacs shortcuts in Bash 4 Text processing: grep, sed, awk String/text functions: length, split, sprintf,
gsub, sub, index, match, tolower, toupper.
(See http://readline.kablamo.org/emacs.html) This is an expanded description of three powerful text
processing tools: grep, sed and awk.
1. Ctrl A moves to the start of the line, 4.4 Regular expressions
2. Ctrl E moves to the end of the line,
3. Ctrl U deletes to the beginning of the line. 4.1 grep pattern search enginge 1. POSIX character classes:
4. Ctrl K deletes to the end of the line. (a) [:alnum:] = [a-zA-Z0-9]
The ed command g/re/p was used to globally search (b) [:alpha:] = [a-zA-Z]
5. Ctrl W deletes to the start of the word. a regular expression and print.
6. Ctrl Y pastes text from the clipboard. (c) [:ascii:] = [\x00-\x7F]
7. Ctrl L clears the screen. grep prints lines matching a pattern: (d) [:blank:] = [ \t]
8. Alt R undoes all changes to the line. c prints a count of matching lines instead, (e) [:cntrl:] = [\x00-\x1F\x7F]
9. Ctrl R searches incrementally up the history. e uses a regexp pattern, (f) [:digit:] = [0-9]
10. Ctrl XE invokes an editor to write complex f obtains patterns from a file, (g) [:graph:] = [\x21-\x7E]
command. i ignores case disctinctions, (h) [:lower:] = [a-z]
v inverts the sense of matching, (i) [:print:] = [\x20-\x7E]
w selects only lines with whole words matches, (j) [:space:] = [ \t\r\n\v\f]
3 Shell style guide n prints line numbers as well, (k) [:word:] = [A-Za-z0-9_]
The following notes are meant to be summary of a A prints num lines of trailing content, (l) [:xdigit:] = [A-Fa-f0-9]
style guide written by Paul Armstrong and too many B prints num lines of leading content, 2. Repetitions:
more to mention (revision 1.26). C prints num lines of both contents, (a) *: 0 or more, +: 1 or more, ?: 0 or 1,
E interprets pattern as an extended regexp, (b) {a, b}: at least a, at most b.
Bash is the only shell scripting language permitted
P interprets pattern as a Perl regexp, 3. Anchors:
for executables. Bash should only be used for simple (a) ^: start of line,
R reads all files under each directory.
wrapper scripts or small utilities. (b) $: end of line,
Executables should have no extension, libraries must 4.2 sed stream editor (c) \<: start of word,
have a .sh extension and should not be executable. (d) \>: end of word.
SUID and SGID are forbidden on shell scripts. sed filters and transforms text: 4. Other:
-e adds a script to the commands to be executed, (a) one|two: one or two,
All error messages should go to STDERR, a function -i edits files in place, (b) (one): a group,
to print out error messages along with other status -n suppresses auto- printing of pattern space, (c) $n: nth group,
information is recommended: -r accepts extended regular expressions. (d) [abcd], [a-d]: ranges,
err () { The simplest usage is sed 's/foo/bar/g' which (e) [^abcd]: negation (not [abcd]).
echo "[$(date +%Y-%m-%d\ %T)]: $@" >&2 substitutes (s) strings globally (g). There are other Awk regex:
} options, including:
/^(ab|c)*[0-9]+/
Comments. Start each file with a description of its a appends line before,
contents. Any function that is in a library or not both d deletes line, Grep regex (with -E flag):
obvious and short, must be commented. Comment i inserts line before, ^(ab|c)*[[:digit:]]+
tricky, interesting or important parts of code. Use p prints line,
TODO comments for temporary or good enough but w writes pattern space to a file. Sed regex:
not perfect code and short-term solutions. sed "s/^\(ab|c\)*[[:digit:]]+/\1\1/"
Default delimiter / can be replaced by any other. This
The following are required for any new code: is useful when regular expression already contains /.
1. Indent 2 spaces, no tabs. Addresses allow limiting to given line numbers:
2. Maximum line length is 80 characters. 1. 1-10 first ten lines
3. Long pipelines should be split one per line. 2. $ the last line
4. Indent case alternatives by 2 spaces. 3. 10~2 even lines starting from the 10th.
5. Always quote strings containing variables,
command substitutions or spaces. One can also use regular expressions:
Use quotes rather than filler characters if possible. sed -e '/:/s/ /_/g'
Use an explicit path when doing wildcard expansion replaces spaces with underscores in lines containing
of filenames. Avoid eval. Use process substitution a colon. Negation may be obtained with !s.
or for loops in preference to piping to while. Finally,
[[ ... ]] is preferred over [ and test.
4.3 awk Aho, Weinberger, Kernighan
Naming conventions. Function and variable names
should be lower case, with underscore to separate awk is a language used as a data extraction and
words. Constants and environment variable names reporting tool.
should be all caps, declared at the top of the file. General form of its code:
Use readonly or declare -r to ensure theyre read
only. Declare function specific variables with local. #! /bin/awk
A function called main is required for scripts long BEGIN {initialization}
enough to contain at least one other function. search pattern {actions} # for example:
/word[0-9]/ {gold += $2} # regex
!/word[0-9]/ {counter++} # negation
END {final actions}
Awk is weakly typed: variables can be treated either as
numeric values or strings, which are not represented
as one-dimensional arrays of characters! Important
variables include:
1. FS: field separator (tab and space by default),
2. OFS: output field separator,
3. RS: record separator (new line),
4. NR: number of the current record,
5. NF: number of fields in the current record.
Numerical functions: int, sqrt, exp, log, sin, cos,
atan2, rand (pseudo random from [0, 1)), srand
(without parameters, uses time of day as a seed).
5 Unix utilities and shell builtins du estimates file space usage: bg resumes suspended jobs in the background.
a writes counts for all files, not just directories, fg resumes suspended jobs in the foreground.
5.1 File system c produces a grand total, jobs lists the active jobs.
cat concatenates and prints files: d the depth at which summing should occur, cmd & runs command in the background.
A shows all nonprinting characters, h prints sizes in human readable format,
crontab maintain individual users crontab
b numbers nonempty output lines, s diplays only a total,
files. See also cron: a daemon that executes
s suppresses repeated empty output lines. X excludes files that match pattern.
scheduled commands.
tac does the same in reverse. file determines file type.
rev reverses lines characterwise. kill sends a TERM signal to a process.
nl numbers lines of files: find searches for files in a directory hierarchy. killall kills processes by name.
s adds string after line number, 1. Tests: ps reports a snapshot of the current processes:
w uses number columns for line numbers. -name base of file name, e selects all processes,
-iname case insensitive name, f does full-format listing,
chgrp changes group ownership. -group, -user ownership
chmod changes permissions of a file: C selects processes by command name,
-perm 755, -perm /u=x permissions p selects processes by PID,
ugoa of the owner, group, other or all users, -size +5M -1G size between 5MB and 1GB
+-= adds, removes or sets selected file mode bits, u selects processes by EUID or name.
-amin -60 accessed in last hour pstree displays a tree of processes.
rwx selects file mode bits: read 4/write 2/execute 1. -cmin, -mmin: created, modified,
chown changes owner of a file. -mtime +7 modified over a week ago nice changes process priority.
umask sets file mode creation mask. -type d directories only,
touch changes file timestamps: pgrep, pkill looks up or signals processes based
-type f files only, on name and other attributes.
a only the access time, -empty empty files or directories only,
m only the modification time, 2. Example (deletes files larger than 5 megabytes): time runs programs and summarizes system
t uses custom stamp instead of current time, find / -size +5M -exec rm -f {} \; resource usage.
c does not create files.
fsck checks and repairs a Linux filesystem: top displays linux processes.
See also: cksum (CRC checksums), md5sum. See also: htop (Hisham top).
a automatically repairs (without any question!),
shasum prints or checks SHA message digests:
t specifies the type(s) of filesystem to be checked,
a algorithm: 1, 224, 256, 384, 512, 512224 or 512256, 5.3 User environment
A tries to check all filesystems in one run,
b reads in binary mode,
M skips mounted filesystems,
c checks SHA sums read from the files. clear clears the terminal screen.
R skips the root filesystem. env runs programs in modified environment.
wc prints newline, word and byte counts (lwc):
m prints the character counts, ln makes hard links between files (only in the exit terminates the calling process.
L prints the maximum display width. same file system, not between directories): finger looks up user information.
s makes symbolic links instead. history displays the history list.
dd converts and copies a file: mesg displays messages from other users.
1. if= reads from a file, ls lists directory contents:
2. of= writes to a file, a does not ignore entries starting with dot, passwd changes user password:
3. bs= up to bytes bytes at a time, F appends indicator to entries, d deletes (empties) an accounts password,
4. count= copies only n input blocks. h prints human readable sizes, e expires an accounts password,
i prints the index number of each file, n minimum days to change password,
cp copies files and directories: w warning days before password expire,
b makes a backup of existing destination files, l prints permissions, number of hard links,
owner, group, size, last-modified date as well, x maximum days a password remains valid.
f removes an existing destination file if needed, pwgen generate pronounceable passwords:
i prompts before overwrite, r reverses order while sorting,
R lists subdirectories recursively, s generates hard to memorize passwords,
n does not overwrite existing files, y includes special characters,
L always follows symlinks in source, S sorts by file size (largest first),
t sorts by modification time (newest first), n includes numbers,
P never follows symlinks in source, N generates num passwords
p preserves timestamps, mode, ownership, tree lists tree-like contents of directories.
r copies directories recursively, mount mounts a filesystem.
su changes user ID or becomes superuser.
s makes symbolic links instead, sudo executes a command as superuser:
l hard links files instead, pwd prints name of current directory. u as a different user.
t copies all source arguments into directory, hostname shows/sets the host name:
pv monitors progress of data through a pipe.
T treats destination as a normal file, i displays the network address.
u copies only newer source files, tar stores and extracts files from a disk archive: uname prints system information:
v explains what is being done. c creates a new archive, a all information, in the following order:
mv moves (renames) files: x extracts files, s the kernel name,
b makes a backup of existing destination files, t lists the contents of an archive, n the network node hostname,
i prompts before overwriting, v verbosely lists files processed, r the kernel release,
f does not prompt before overwriting, j bzip2 compression, v the kernel version,
n does not overwrite existing destination files. z uses zip/gzip (gz compression), m the machine hardware name,
t moves all source arguments into directory, f uses archive file or device (???), p the processor type,
T treats destination as a normal file, k does not replace existing files when extracting. i the hardware platform,
u moves only newer source files, o the operating system.
v explains what is being done. tee duplicates pipe content:
rm removes files or directories: a appends to the given files, does not overwrite, uptime: how long has system been running?
f never prompts, i ignores interrupts.
wall writes a message to all users,
i always prompts, write sends a message to another user.
r removes directories and their contents. 5.2 Processes
See also: rmdir (directories removal), shred. who shows who is logged on,
mkdir makes directories (p: with parents as chroot changes the root directory of the calling w does the same, shows what they are doing,
needed, no error if existing). process and their children. whoami prints effective userid.
df reports file system disk space usage: at schedules commands to be executed once,
h prints size in powers of 1024, at a particular time in the future: it accepts
i list inode information instead of block usage, times of the form HH:MM, midnight, noon
t limits listing to file systems of given type, or teatime; MMDD[CC]YY, MM/DD/[CC]YY,
x limits listing to file systems not of given type, DD.MM.[CC]YY or [CC]YY-MM-DD (the speci-
T prints file systems types. fication of a date must follow the specification
of the time of day). You can also give times like
now + 3 hours.
5.4 Text processing I replaces occurrences of string with names route shows and manipulates the IP routing
read from standard input. table.
awk, grep and sed have been described earlier. traceroute is a computer network diagnostic
yes outputs a string repeatedly until killed. tool for displaying the route (path) and mea-
comm compares two sorted files line by line.
shuf generates random permutations: suring transit delays of
e treats each arg as an input line, 5.5 Shell builtins
i treats each number .. through .. as an input alias allows a string to be substituted for a
5.7 Searching
line, word. find searches for files in a directory hierarchy.
n outputs at most count lines, cd changes the shell working directory: locate finds files by names.
r output lines can be repeated (with -n). - to the previous directory. updatedb updates the file database used by lo-
sort sorts lines of text files: echo* displays a line of text: cate.
c checks for sorted input, e enables interpretation of backslash escapes, whatis displays one-line manual page descrip-
f folds lower case to upper case characters, n does not output the trailing newline. tion.
g compares general numerical values, test checks file types and compares values. whereis locates the binary, source, and manual
h compares human readable numbers, unset unsets a shell variable, removing it from page files for a command.
k sorts via a key, memory and the shells exported environment.
n compares string numerical values, wait waits for process to change state.
r reverses the results,
5.8 Hardware
s stabilizes the sort. dmesg prints/controls the kernel ring buffer.
tsort performs topological sort. 5.6 Networking lsblk lists block devices.
uniq omits repeated lines: lsof lists open files.
curl transfers a URL.
c prefixes lines by the number of occurences, lsusb listsq USB devices.
wget is a non-interactive network downloader.
d only prints duplicate lines, one for each group,
A, R specifies lists of file suffixes or patterns (when
f avoids comparing first fields, 5.9 For programmers
wildcard characters appear) to accept or reject,
i ignores differences in case,
b goes to background immediately after startup, g++ compiles, assembles and links C++ files:
s avoids comparing first characters,
c continues getting a partially-downloaded file, o writes the build output to a file named . . .
w compares no more than n characters.
m turns on options suitable for mirroring: infi-
cut prints selected parts of lines: nite recursion and time-stamping,
5.10 Miscellaneous
--complement complements the selection, np does not ever ascend to the parent directory
c selects only these characters, when retrieving recursively, bc is an arbitrary precision calculator lan-
d uses delim instead of Tab for field delimeter, U identifies as agent-string to the HTTP server. guage.
f selects only these fields, w waits the specified number of seconds between 1. echo obase=16;255 | bc prints FF,
s does not print lines not containing delimeters. the retrievals (see also random-wait). 2. echo ibase=2;obase=A;10 | bc prints
join joins lines of two files on a common field. 2,
rlogin starts a terminal session on a remote 3. scale=10 (after bc -l) sets working preci-
paste merges lines of files.
host. sion.
d reuses characters from list instead of tabs,
ssh is an OpenSSH SSH client (remote login dc is a reverse-polish desk calculator. One of
s pastes one file at a time, not in parallel.
program). the oldest Unix utilities, predating even the in-
tr translates or deletes characters:
D specifies a local dynamic application-level vention of the C programming language.
c uses the complement of set1,
port forwarding, cal, ncal displays a calendar.
d deletes characters, does not translate,
p selects a port to connect to on the remote host, e displays date of Easter,
s replaces each sequence of a repeated character
X enables X11 forwarding. j displays Julian days,
that is listed in the last specified set with a
single occurrence of that character. dig interrogates DNS name servers. m displays the specified month,
x performs a simplified reverse lookup. w prints the numbers of the weeks,
diff compares files line by line:
host is a DNS lookup utility. y displays a calendar for the specified year,
y outputs in two columns, 3 displays the previous, current and next month.
nslookup is (probably) deprecated! Use dig
i ignores case differences, date prints or set the system date and time.
and host.
w ignores all white space. seq prints a sequence of numbers:
ifconfig configures a network interface. w equalizes width by padding with leading ze-
fmt is a simple optimal text formatter,
inetd is a super-server daemon that provides roes.
fold wraps each line to fit in specified width.
Internet services. sleep delays for a specified amount of time.
head outputs the first (last) part of files: netcat: arbitrary TCP and UDP connections true, false does nothing, (un)successfully.
c the first num bytes, and listens.
n the first num lines, netstat prints network connections, routing
tail the last num bytes: tables, interface statistics, masquerade connec-
c the last num bytes, tions, and multicast memberships.
n the last num lines, ping tests the reachability of a host on an IP
f outputs appended data as the file grows, network by sending ICMP ECHO_REQUEST:
s sleeps for n seconds between iterations. c stops after sending count packets,
split splits a file into pieces: n numeric output only, avoids to lookup sym-
a generates suffixes of length n (default 2), bolic names for host addresses.
b puts size bytes per output file, rdate sets the systems date from a remote host.
d uses numeric (not alphabetic) suffixes, rsync copies files fast (remote or local):
l puts number lines/records per output file, a in archive mode, equivalent to:
n generates chunks output files. g preserves group,
See also: csplit. o preserves owner (super-user only)
p preserves permissions,
more pages text too large to fit on one screen t preserves modification times,
and allows scrolling down, but not up and l copies symlinks as symlinks,
therefore is deprecated. b make backups,
less is an enhanced version of more: c skip based on checksum,
+F monitors the tail of a file which is growing. n performs a dry run without changes made,
vim is an advanced text editor, too complex to r resursively,
be explained here. See also emacs. u skip newer files on the receiver,
v increases verbosity,
xargs builds and executes command lines: z compresses file data during the transfer,
0 takes care of filenames with spaces, back- delete deletes extraneous files from dest
slashes. dirs.