Published on

Bash Cheat Sheet - Master Bash Scripting

Bash: CheetSheet

What is Bash ?

Bash is a powerful programming language, one perfectly designed for use on the command line and in shell scripts. Bash can be used to automate software development tasks such as code compilation, debugging source code, change management and software testing.

In this article we will discuss about common Bash Commands and their usase-

Bash Shortcut Commands

CommandExplanation
CTRL+AMove to beginning of line
CTRL+BMoves backward one character
CTRL+CHalts the current command
CTRL+DDeletes one character backward or logs out of current session, similar to exit
CTRL+EMoves to end of line
CTRL+FMoves forward one character
CTRL+GAborts the current editing command and ring the terminal bell
CTRL+HDeletes one character under cursor (same as DELETE)
CTRL+JSame as RETURN
CTRL+KDeletes (kill) forward to end of line
CTRL+LClears screen and redisplay the line
CTRL+MSame as RETURN
CTRL+NNext line in command history
CTRL+OSame as RETURN, then displays next line in history file
CTRL+PPrevious line in command history
CTRL+QResumes suspended shell output
CTRL+RSearches backward
CTRL+SSearches forward or suspends shell output
CTRL+TTransposes two characters
CTRL+UKills backward from point to the beginning of line
CTRL+VMakes the next character typed verbatim
CTRL+WKills the word behind the cursor
CTRL+XLists the possible filename completions of the current word
CTRL+YRetrieves (yank) last item killed
CTRL+ZStops the current command, resume with fg in the foreground or bg in the background
ALT+BMoves backward one word
ALT+DDeletes next word
ALT+FMoves forward one word
ALT+HDeletes one character backwar
ALT+TTransposes two words
ALT+.Pastes last word from the last command. Pressing it repeatedly traverses through command history.
ALT+UCapitalizes every character from the current cursor position to the end of the word
ALT+LUncapitalizes every character from the current cursor position to the end of the word
ALT+CCapitalizes the letter under the cursor. The cursor then moves to the end of the word.
ALT+RReverts any changes to a command you’ve pulled from your history if you’ve edited it.
ALT+?List possible completions to what is typed
ALT+^Expand line to most recent match from history
CTRL+X then (Start recording a keyboard macro
CTRL+X then )Finish recording keyboard macro
CTRL+X then ERecall last recorded keyboard macro
CTRL+X then CTRL+EInvoke text editor (specified by $EDITOR) on current command line then execute resultes as shell commands
CTRL+A then DLogout from screen but don't kill it, if any command exist, it will continue
BACKSPACEDeletes one character backward
DELETEDeletes one character under cursor
historyShows command line history
!!Repeats the last command
!<n>Refers to command line n
!<string>Refers to command starting with string
esc :wqExits and saves script
exitLogs out of current session

BASH Basic Commands

CommandExplanation
envDisplays all environment variables
echo $SHELLDisplays the shell you're using
echo $BASH_VERSIONDisplays bash version
bashIf you want to use bash (type exit to go back to your previously opened shell)
whereis bashLocates the binary, source and manual-page for a command
which bashFinds out which program is executed as 'bash' (default: /bin/bash, can change across environments)
clearClears content on window (hide displayed lines)

BASH FILE COMMANDS

CommandExplanation
lsLists your files in current directory, ls <dir> to print files in a specific directory
ls -lLists your files in 'long format', which contains the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified
ls -aLists all files in 'long format', including hidden files (name beginning with '.')
ln -s <filename> <link>Creates symbolic link to file
readlink <filename>Shows where a symbolic links points to
treeShow directories and subdirectories in easilly readable file tree
mcTerminal file explorer (alternative to ncdu)
touch <filename>Creates or updates (edit) your file
mktemp -t <filename>Make a temp file in /tmp/ which is deleted at next boot (-d to make directory)
cat <filename>Displays file raw content (will not be interpreted)
cat -n <filename>Shows number of lines
nl <file.sh>Shows number of lines in file
cat filename1 > filename2Copy filename1 to filename2
cat filename1 >> filename2Merge two files texts together
any_command > <filename>'>' is used to perform redirections, it will set any_command's stdout to file instead of "real stdout" (generally /dev/stdout)
more <filename>Shows the first part of a file (move with space and type q to quit)
head <filename>Outputs the first lines of file (default: 10 lines)
tail <filename>Outputs the last lines of file (useful with -f option) (default: 10 lines)
vim <filename>Opens a file in VIM (VI iMproved) text editor, will create it if it doesn't exist
mv <filename1> <dest>Moves a file to destination, behavior will change based on 'dest' type (dir: file is placed into dir; file: file will replace dest (tip: useful for renaming))
cp <filename1> <dest>Copies a file
rm <filename>Removes a file
find . -name <name> <type>Searches for a file or a directory in the current directory and all its sub-directories by its name
diff <filename1> <filename2>Compares files, and shows where they differ
wc <filename>Tells you how many lines, words and characters there are in a file. Use -lwc (lines, word, character) to ouput only 1 of those informations
sort <filename>Sorts the contents of a text file line by line in alphabetical order, use -n for numeric sort and -r for reversing order.
sort -t -k <filename>Sorts the contents on specific sort key field starting from 1, using the field separator t.
revReverse string characters (hello becomes olleh)
chmod -options <filename>Lets you change the read, write, and execute permissions on your files (more infos: SUID, GUID)
gzip <filename>Compresses files using gzip algorithm
gunzip <filename>Uncompresses files compressed by gzip
gzcat <filename>Lets you look at gzipped file without actually having to gunzip it
lpr <filename>Prints the file
lpqChecks out the printer queue
lprm <jobnumber>Removes something from the printer queue
genscriptConverts plain text files into postscript for printing and gives you some options for formatting
dvips <filename>Prints .dvi files (i.e. files produced by LaTeX)
grep <pattern> <filenames>Looks for the string in the files
grep -r <pattern> <dir>Search recursively for pattern in directory
head -n file_name | tail +nPrint nth line from file.
head -y lines.txt | tail +xWant to display all the lines from x to y. This includes the xth and yth lines.
sed 's/<pattern>/<replacement>/g' <filename>Replace pattern in file with replacement value to std output the character after s (/) is the delimeter
sed -i 's/<pattern>/<replacement>/g' <filename>Replace pattern in file with replacement value in place
echo "this" | sed 's/is/at/g'Replace pattern from input stream with replacement value

BASH DIRECTORY COMMANDS

CommandExplanation
mkdir <dirname>makes a new directory
rmdir <dirname>remove an empty directory
rmdir -rf <dirname>remove a non-empty directory
mv <dir1> <dir2>rename a directory from <dir1> to <dir2>
cd**changes to home
cd ..changes to the parent directory
cd <dirname>**changes directory
cp -r <dir1> <dir2>copy <dir1> into <dir2> including sub-directories
pwdtells you where you currently are
cd ~**changes to home.
cd -changes to previous working directory

BASH SSH, SYSTEM INFO & NETWORK COMMANDS

CommandExplanation
ssh user@hostConnects to host as user
ssh -p <port> user@hostConnects to host on specified port as user
ssh-copy-id user@hostAdds your ssh key to host for user to enable a keyed or passwordless login
whoamiReturns your username
su <user>Switch to a different user
su -Switch to root, likely needs to be sudo su -
sudo <command>Execute command as the root user
passwdLets you change your password
quota -vShows what your disk quota is
dateShows the current date and time
calShows the month's calendar
uptimeShows current uptime
wDisplays whois online
finger <user>Displays information about user
uname -aShows kernel information
man <command>Shows the manual for specified command
info <command>Shows another documentation system for the specific command
helpShows documentation about built-in commands and functions
dfShows disk usage
du <filename>Shows the disk usage of the files and directories in filename (du -s give only a total)
resize2fsext2/ext3/ext4 file system resizer
last <yourUsername>Lists your last logins
ps -u yourusernameLists your processes
kill <PID>Kills the processes with the ID you gave
killall <processname>Kill all processes with the name
topDisplays your currently active processes
lsofLists open files
bgLists stopped or background jobs ; resume a stopped job in the background
fgBrings the most recent job in the foreground
fg <job>Prings job to the foreground
ping <host>Pings host and outputs results
whois <domain>Gets whois information for domain
dig <domain>Gets DNS information for domain
dig -x <host>Reverses lookup host
wget <file>Downloads file
netstatPrint network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
time <command>Report time consumed by command execution

BASH VARIABLES

CommandExplanation
varname=valuedefines a variable
varname=value commanddefines a variable to be in the environment of a particular subprocess
echo $varnamechecks a variable's value
echo $$prints process ID of the current shell
echo $!prints process ID of the most recently invoked background job
echo $?displays the exit status of the last command
read <varname>reads a string from the input and assigns it to a variable
read -p "prompt" <varname>same as above but outputs a prompt to ask user for value
column -t <filename>display info in pretty columns (often used with pipe)
let <varname> = <equation>*performs mathematical calculation using operators like +, -, , /, %
export VARNAME=valuedefines an environment variable (will be available in subprocesses)
export -f <funcname>Exports function 'funcname'
export var1="var1 value"Export and assign in the same statement
export <varname>**Copy Bash variable **
declare -x <varname>**Copy Bash variable **
array[0]=valA, array[1]=valB, array[2]=valChow to define an array
array=([2]=valC [0]=valA [1]=valB)another way
array=(valA valB valC)and another
${array[i]}displays array's value for this index. If no index is supplied, array element 0 is assumed
${#array[i]}to find out the length of any element in the array
${#array[@]}to find out how many values there are in the array
declare -athe variables are treated as arrays
declare -fuses function names only
declare -Fdisplays function names without definitions
declare -ithe variables are treated as integers
declare -rmakes the variables read-only
declare -xmarks the variables for export via the environment
declare -luppercase values in the variable are converted to lowercase
declare -Amakes it an associative array
${varname:-word}if varname exists and isn't null, return its value; otherwise return word
${varname:word}if varname exists and isn't null, return its value; otherwise return word
${varname:=word}if varname exists and isn't null, return its value; otherwise set it word and then return its value
${varname:?message}if varname exists and isn't null, return its value; otherwise print varname, followed by message and abort the current command or script
${varname:+word}if varname exists and isn't null, return word; otherwise return null
${varname:offset:length}performs substring expansion. It returns the substring of $varname starting at offset and up to length characters
${variable#pattern}if the pattern matches the beginning of the variable's value, delete the shortest part that matches and return the rest
${variable##pattern}if the pattern matches the beginning of the variable's value, delete the longest part that matches and return the rest
${variable%pattern}if the pattern matches the end of the variable's value, delete the shortest part that matches and return the rest
${variable%%pattern}if the pattern matches the end of the variable's value, delete the longest part that matches and return the rest
${variable/pattern/string}the longest match to pattern in variable is replaced by string. Only the first match is replaced
${variable//pattern/string}the longest match to pattern in variable is replaced by string. All matches are replaced
${#varname}returns the length of the value of the variable as a character string
*(patternlist)matches zero or more occurrences of the given patterns
+(patternlist)matches one or more occurrences of the given patterns
?(patternlist)matches zero or one occurrence of the given patterns
@(patternlist)matches exactly one of the given patterns
!(patternlist)matches anything except one of the given patterns
$(UNIX command)command substitution: runs the command and returns standard output
typeset -l <x>makes variable local - <x> must be an interger

BASH FUNCTIONS

The function refers to passed arguments by position (as if they were positional parameters), that is, 1,1, 2, and so forth.

$@ is equal to "$1" "$2"... "$N", where N is the number of positional parameters. $# holds the number of positional parameters.

function functname() {
  shell commands
}
unset -f functname  # deletes a function definition
declare -f          # displays all defined functions in your login session

BASH FLOW CONTROLS

CommandExplanation
statement1 && statement2and operator
statement1 || statement2or operator
-aand operator inside a test conditional expression
-oor operator inside a test conditional expression

BASH STRINGS

CommandExplanation
str1 == str2str1 matches str2
str1 != str2str1 does not match str2
str1 < str2str1 is less than str2 (alphabetically)
str1 > str2str1 is greater than str2 (alphabetically)
str1 \> str2str1 is sorted after str2
str1 \< str2str1 is sorted before str2
-n str1str1 is not null (has length greater than 0)
-z str1str1 is null (has length 0)

BASH FILES

CommandExplanation
-a filefile exists or its compilation is successful
-d filefile exists and is a directory
-e filefile exists; same -a
-f filefile exists and is a regular file (i.e., not a directory or other special type of file)
-r fileyou have read permission
-s filefile exists and is not empty
-w fileyour have write permission
-x fileyou have execute permission on file, or directory search permission if it is a directory
-N filefile was modified since it was last read
-O fileyou own file
-G filefile's group ID matches yours (or one of yours, if you are in multiple groups)
file1 -nt file2file1 is newer than file2
file1 -ot file2file1 is older than file2

BASH NUMBERS

CommandExplanation
-ltless than
-leless than or equal
-eqequal
-gegreater than or equal
-gtgreater than
-nenot equal
if condition
then
statements
[elif condition
then statements...]
[else
statements]
fi

for x in {1..10}
do
statements
done

for name [in list]
do
statements that can use $name
done

for (( initialisation ; ending condition ; update ))
do
statements...
done

case expression in
pattern1 )
    statements ;;
pattern2 )
    statements ;;
esac

select name [in list]
do
statements that can use $name
done

while condition; do
statements
done

until condition; do
statements
done

BASH COMMAND-LINE PROCESSING CYCLE

The default order for command lookup is functions, followed by built-ins, with scripts and executables last. There are three built-ins that you can use to override this order: command, builtin and enable.

CommandExplanation
commandremoves alias and function lookup. Only built-ins and commands found in the search path are executed
builtinlooks up only built-in commands, ignoring functions and commands found in PATH
enableenables and disables shell built-ins
evaltakes arguments and run them through the command-line processing steps all over again

BASH INPUT/OUTPUT REDIRECTORS

CommandExplanation
cmd1|cmd2pipe; takes standard output of cmd1 as standard input to cmd2
< filetakes standard input from file
> filedirects standard output to file
>> filedirects standard output to file; append to file if it already exists
>|fileforces standard output to file even if noclobber is set
n>|fileforces output to file from file descriptor n even if noclobber is set
<> fileuses file as both standard input and standard output
n<>fileuses file as both input and output for file descriptor n
n>filedirects file descriptor n to file
n<filetakes file descriptor n from file
n>>filedirects file description n to file; append to file if it already exists
n>&duplicates standard output to file descriptor n
n<&duplicates standard input from file descriptor n
n>&mfile descriptor n is made to be a copy of the output file descriptor
n<&mfile descriptor n is made to be a copy of the input file descriptor
&>filedirects standard output and standard error to file
<&-closes the standard input
>&-closes the standard output
n>&-closes the ouput from file descriptor n
n<&-closes the input from file descriptor n
|tee <file>output command to both terminal and a file (-a to append to file)

BASH PROCESS HANDLING

To suspend a job, type CTRL+Z while it is running. You can also suspend a job with CTRL+Y. This is slightly different from CTRL+Z in that the process is only stopped when it attempts to read input from terminal. Of course, to interrupt a job, type CTRL+C.

CommandExplanation
myCommand &runs job in the background and prompts back the shell
jobslists all jobs (use with -l to see associated PID)
fgbrings a background job into the foreground
fg %+brings most recently invoked background job
fg %-brings second most recently invoked background job
fg %Nbrings job number N
fg %stringbrings job whose command begins with string
fg %?stringbrings job whose command contains string
kill -lreturns a list of all signals on the system, by name and number
kill PIDterminates process with specified PID
kill -s SIGKILL 4500sends a signal to force or terminate the process
kill -15 913Ending PID 913 process with signal 15 (TERM)
kill %1Where %1 is the number of job as read from 'jobs' command.
psprints a line of information about the current running login shell and any processes running under it
ps -aselects all processes with a tty except session leaders
trap cmd sig1 sig2executes a command when a signal is received by the script
trap "" sig1 sig2ignores that signals
trap - sig1 sig2resets the action taken when the signal is received to the default
disown <PID|JID>removes the process from the list of jobs
waitwaits until all background jobs have finished
sleep <number>wait # of seconds before continuing
pv**display progress bar for data handling commands. often used with pipe like |pv **
yesgive yes response everytime an input is requested from script/process

BASH TIPS & TRICKS

  • Set an alias

    cd; nano .bash_profile

alias gentlenode='ssh admin@gentlenode.com -p 3404' # add your alias in .bash_profile

  • To quickly go to a specific directory

    cd; nano .bashrc
    > shopt -s cdable_vars
    > export websites="/Users/mac/Documents/websites"

    source .bashrc
    cd $websites

DEBUGGING SHELL PROGRAMS IN BASH

CommandExplanation
bash -n scriptnamedon't run commands; check for syntax errors only
set -o noexecalternative (set option in script)
bash -v scriptnameecho commands before running them
set -o verbosealternative (set option in script)
bash -x scriptnameecho commands after command-line processing
set -o xtracealternative (set option in script)
  • trap 'echo $varname' EXIT - useful when you want to print out the values of variables at the point that your script exits|

    function errtrap {
        es=$?
        echo "ERROR line $1: Command exited with status $es."
    }
    
  • trap 'errtrap $LINENO' ERR - is run whenever a command in the surrounding script or function exits with non-zero status

    function dbgtrap {
        echo "badvar is $badvar"
    }
    
  • trap dbgtrap DEBUG - causes the trap code to be executed before every statement in a function or script ...section of code in which the problem occurs...

  • trap - DEBUG - turn off the DEBUG trap

        function returntrap {
        echo "A return occurred"
    }
    
  • trap returntrap RETURN - is executed each time a shell function or a script executed with the . or source commands finishes executing

BASH TEXT COLORS AND BACKGROUNDS

  • note: \e or \x1B also work instead of \033

  • Color_Off='\033[0m' - Text Reset

  • Regular Colors

    Black='\033[0;30m'  # Black
    Red='\033[0;31m'    # Red
    Green='\033[0;32m'  # Green
    Yellow='\033[0;33m' # Yellow
    Blue='\033[0;34m'   # Blue
    Purple='\033[0;35m' # Purple
    Cyan='\033[0;36m'   # Cyan
    White='\033[0;97m'  # White
    
  • Additional colors

    LGrey='\033[0;37m'  # Ligth Gray
    DGrey='\033[0;90m'  # Dark Gray
    LRed='\033[0;91m'   # Ligth Red
    LGreen='\033[0;92m' # Ligth Green
    LYellow='\033[0;93m'# Ligth Yellow
    LBlue='\033[0;94m'  # Ligth Blue
    LPurple='\033[0;95m'# Light Purple
    LCyan='\033[0;96m'  # Ligth Cyan
    
  • Bold

    BBlack='\033[1;30m' # Black
    BRed='\033[1;31m'   # Red
    BGreen='\033[1;32m' # Green
    BYellow='\033[1;33m'# Yellow
    BBlue='\033[1;34m'  # Blue
    BPurple='\033[1;35m'# Purple
    BCyan='\033[1;36m'  # Cyan
    BWhite='\033[1;37m' # White
    
  • Underline

    UBlack='\033[4;30m' # Black
    URed='\033[4;31m'   # Red
    UGreen='\033[4;32m' # Green
    UYellow='\033[4;33m'# Yellow
    UBlue='\033[4;34m'  # Blue
    UPurple='\033[4;35m'# Purple
    UCyan='\033[4;36m'  # Cyan
    UWhite='\033[4;37m' # White
    
  • Background

    On_Black='\033[40m' # Black
    On_Red='\033[41m'   # Red
    On_Green='\033[42m' # Green
    On_Yellow='\033[43m'# Yellow
    On_Blue='\033[44m'  # Blue
    On_Purple='\033[45m'# Purple
    On_Cyan='\033[46m'  # Cyan
    On_White='\033[47m' # White
    
  • Example of usage

    echo -e "${Green}This is GREEN text${Color_Off} and normal text"
    echo -e "${Red}${On_White}This is Red test on White background${Color_Off}" 
    
    printf "${Red} This is red \n"
    

option -e is mandatory, it enable interpretation of backslash escapes

  1. Dynamic variable names in Bash
  2. How to parse command line arguments in Bash?
  3. Bash and shell script variable capitalization?
  4. How to get the directory where a Bash script is located from within the script itself?
  5. Parsing JSON with Unix tools
  6. Command not found error in Bash variable assignment
  7. How to set a variable to the output of a command in Bash?
  8. How to run Bash commands in Python?
  9. Extract filename and extension in Bash? 10.Looping through the content of a file in Bash