Bash Syntax Cheat Sheet

Artistic digital illustration of Bash Syntax Cheat Sheet focusing on essential Bash syntax and commands

Managing dedicated servers and performing system administration tasks at IOFLOOD requires a solid understanding of Bash syntax to efficiently automate processes and troubleshoot issues. Drawing from our experience in server management and software deployment, we’ve compiled this comprehensive Bash reference guide.

This Bash Cheat Sheet is your concise companion in the vast world of Bash programming. With a dedicated reference section for commands and key programming concepts, followed by practical example code snippets, this cheat sheet is designed to assist both beginners and seasoned professionals alike.

So, let’s set sail on this exciting journey to unlock the full potential of Bash. Ready your terminals, and let’s dive into Bash!

Command Reference Section

Before diving into practical examples, let’s explore the various commands and concepts of Bash. This reference section will also provide explanations and example usage cases when applicable. These topics will serve as key tools while scripting in Bash and will also deepen your understanding of the following Sample Code.

Command Chaining

Command/ConceptDescriptionSyntax Example
&&Executes a secondary command only if the first command succeeds.command1 && command2
||Or Operator executes secondary command only if the first command fails.command1 || command2
;Executes all commands in sequence, regardless of their exit status.command1; command2
|Pipes the output of the first command to the input of the second command.command1 \| command2
&Runs the command in the background, allowing the terminal to accept new commands immediately.command1 &

Common Commands

CommandDescriptionExample Usage
lsLists the contents of a -l lists in long format.
cdChanges the current /home changes to the /home directory.
pwdPrints the current working directory.pwd displays the current directory path.
mkdirCreates a new directory.mkdir new_dir creates a new directory named new_dir.
rmRemoves files or directories.rm file.txt removes file.txt. Use -r for directories.
echoDisplays a line of text.echo "Hello World" prints Hello World.
catConcatenates and displays file.txt displays the contents of file.txt.
touchCreates an empty file or updates the timestamp.touch new_file.txt creates or updates new_file.txt.
cpCopies files and directories.cp source.txt dest.txt copies source.txt to dest.txt.
mvMoves or renames files and old_name.txt new_name.txt renames/moves a file.
grepSearches for patterns in files.grep "search_term" file.txt searches for “search_term” in file.txt.
findSearches for files in a directory hierarchy.find . -name "file.txt" finds all file.txt in the current directory and subdirectories.
chmodChanges the file mode (permissions).chmod 755 sets to be readable and executable by everyone, but only writable by the file owner.
chownChanges file owner and group.chown user:group file.txt changes the owner and group of file.txt.

Command Modifiers

Modifier Flags for ls

FlagDescriptionExample Usage
-lLists files in long format, showing detailed -l
-aIncludes directory entries whose names begin with a dot (.).ls -a
-hHuman-readable format, with file -lh
-rReverses the order of the -lr
-tSorts by modification time, newest -lt

Modifier Flags for grep

FlagDescriptionExample Usage
-iIgnores case distinctions in both the pattern and the input files.grep -i "pattern" file
-vInverts the match, showing lines that do not match the given pattern.grep -v "pattern" file
-cCounts the number of lines that match the pattern.grep -c "pattern" file
-nPrefixes each line of output with the line number within its input file.grep -n "pattern" file
-rRecursively searches for the pattern in all files under the specified directory.grep -r "pattern" directory

Modifier Flags for mkdir

FlagDescriptionExample Usage
-mSets the file mode (permissions) of the newly created directory.mkdir -m 755 newdir
-pCreates parent directories as needed and does not return an error if the directory already exists.mkdir -p /path/to/newdir

Modifier Flags for rm

FlagDescriptionExample Usage
-r or -RRemoves directories and their contents recursively.rm -r folderName
-fForces deletion without prompting for confirmation.rm -f filename
-iPrompts for confirmation before deleting each file.rm -i filename

Modifier Flags for echo

FlagDescriptionExample Usage
-nDo not output the trailing newline.echo -n "text"
-eEnable interpretation of backslash escapes.echo -e "Line 1\nLine 2"

Modifier Flags for cat

FlagDescriptionExample Usage
-nNumber all output -n file
-bNumber non-empty output -b file
-sSuppress repeated empty output -s file

Modifier Flags for touch

FlagDescriptionExample Usage
-aChanges only the access time.touch -a file
-mChanges only the modification time.touch -m file
-cDoes not create any files that do not already exist.touch -c file

Modifier Flags for cp

FlagDescriptionExample Usage
-r or -RRecursively copies directories.cp -r sourceDir targetDir
-iPrompts before overwrite.cp -i sourceFile targetFile
-vVerbose mode, shows files as they are copied.cp -v sourceFile targetFile
-aArchives files, copying all file attributes, including permissions and symbolic links.cp -a sourceDir targetDir

Modifier Flags for mv

FlagDescriptionExample Usage
-iPrompts before -i oldName newName
-vVerbose mode, shows files as they are -v oldLocation newLocation
-uMoves only when the source file is newer than the destination file or when the destination file does not -u oldFile newFile

Modifier Flags for find

FlagDescriptionExample Usage
-nameSearch for files by name.find . -name "filename.txt"
-typeSearch for files of a particular type (e.g., d for directories, f for regular files).find . -type f
-permSearch for files with specific permissions.find . -perm 644
-userSearch for files owned by a specific user.find . -user username
-mtimeSearch for files modified a certain number of days ago.find . -mtime +7
-execExecute a command on each file found.find . -type f -exec chmod 644 {} \;
-sizeFinds files of a specific size (e.g., +50M for files over 50MB, -50M for files under 50MB).find . -size +50M

Modifier Flags for chmod

FlagDescriptionExample Usage
+Adds a permission to a file or directory.chmod +x file
-Removes a permission from a file or directory.chmod -x file
rRead permission.chmod +r file (adds read permission)
wWrite permission.chmod +w file (adds write permission)
xExecute permission.chmod +x file (adds execute permission)
u, g, oSet permissions for the user (u), group (g), or others (o).chmod u+x file.txt
aSet permissions for all users.chmod a+w file.txt

Modifier Flags for chown

FlagDescriptionExample Usage
--recursive or -RChange file owner and group recursively.chown -R user:group directory
--verboseOutput a diagnostic for every file processed.chown --verbose user file
--fromChanges the owner and/or group of each given file only if its current owner and/or group match those specified.chown --from=current_user:current_group new_user:new_group file
-cLike verbose but report only when a change is made.chown -c new_user file
-vVerbose mode. Outputs a diagnostic for every file processed.chown -v new_user file


-eExit immediately if a command exits with a non-zero status.
-xPrint commands and their arguments as they are executed.
-uTreat unset variables as an error when substituting.
-o pipefailCauses a pipeline to return the exit status of the last command in the pipe that failed.
-vPrint shell input lines as they are read.
-nRead commands but do not execute them (syntax check).
-fDisable filename expansion (globbing).
-iInteractive mode – even if the input is not coming from a terminal.
-cRead and execute commands from the following string.

Command Line Parameters

$0The filename of the current script.
$1 to $9The positional parameters, representing arguments passed to the script. $1 is the first argument, $2 is the second, and so on.
${10}, ${11}, …For positional parameters beyond 9, curly braces are used to delimit the parameter number.
$#The number of positional parameters passed to the script.
$*All positional parameters seen as a single word. Useful when you need to pass all parameters to another command.
$@All positional parameters as separate strings. When quoted as "$@", each parameter is quoted separately, which is useful for preserving spaces in parameters.
$?The exit status of the last command executed.
$$The process ID (PID) of the current script.
$!The PID of the last background command.
$-The current options set for the shell.
$IFSThe Internal Field Separator that is used by the shell to determine how it recognizes fields, or word boundaries, in a string.
shiftShifts positional parameters to the left by 1, decreasing the positional parameters count ($#). Useful for iterating over all arguments.
setUsed to set or unset options and positional parameters. set -- can be used to overwrite all positional parameters.
getoptsA utility to parse positional parameters. Used in scripts to process command-line options and flags.

Sorting Methods

Technique/CommandDescriptionExample Usage
sort CommandSorts the lines of a text file. Supports numerical, reverse, and case-insensitive sorting among others.sort file.txt for alphabetical sorting.
sort -n file.txt for numerical sorting.
sort with UniqueCombines sorting with removing duplicate lines.sort -u file.txt to sort and remove duplicates.
Array SortingBash does not natively support array sorting, but you can use external commands or custom functions.IFS=$'\n' sorted=($(sort <<<"${array[*]}"))
sort with Reverse OrderSorts in reverse order.sort -r file.txt for reverse alphabetical sorting.
sort with Custom FieldSorts based on a specific field or column.sort -k2,2 file.txt to sort by the second column.
Using awk for SortingPreprocessing with awk before sorting, useful for complex data manipulation.awk '{print $2, $1}' file.txt | sort to swap columns before sorting.
Numeric SortSpecifically sorts lines by numerical value.sort -n file.txt
Version SortSorts lines by version number, useful for filenames or version tags.sort -V files.txt for version sorting.

Data Types

Data TypeDescriptionExample Usage
StringText or characters. Bash does not distinguish between strings and numbers, but strings can contain any character.str="Hello World"
IntegerWhole numbers. Bash supports integer operations and; let "int2 = int * 2"
ArrayIndexed or associative collections of values.Indexed: arr=(one two three)
Associative: declare -A assoc_arr=(["key"]="value")
Command SubstitutionExecutes a command and substitutes its output.result=$(ls)
Arithmetic ExpansionEvaluates an arithmetic expression and substitutes the$((3 + 2))
File DescriptorA reference to an open file. Bash scripts can read from and write to file descriptors.exec 3<file.txt (opens file.txt on FD 3)
Here DocumentA type of redirection that feeds a command list or a script a literal block of <<EOF
Example text
Here StringA form of redirection that feeds a string into the standard input of a command.grep "search" <<< "search in this string"

Boolean Operations

ConceptDescriptionExample Usage
Boolean VariablesBash doesn’t have a built-in Boolean type. Use strings or exit statuses to represent Booleans.is_true="true"; if [ "$is_true" = "true" ]; then echo "True"; fi
Conditional ExecutionUse && for AND and || for OR in command sequences.[[ $a -eq $b ]] && echo "Equal"
[[ $a -ne $b ]] || echo "Not equal"
if StatementsEvaluate conditions using if. Combine with &&, ||, -eq, -ne, etc., for Boolean logic.if [[ $a -gt $b ]] && [[ $c -eq 10 ]]; then echo "True"; fi
test or '[ ]'The test command or '[ ]' evaluates an expression and returns an exit status.test $a -lt $b; echo $?
[ $a -lt $b ]; echo $?
[[ ]] for Advanced ConditionsAdvanced test command that allows for pattern matching and more complex expressions.[[ $a -gt $b && $c -eq 10 ]]
Exit StatusesUse the exit status of commands as Boolean expressions. A status of 0 (success) is true; non-zero is false.command; if [ $? -eq 0 ]; then echo "Success"; fi

Function Structure

PartDescriptionExample Usage
Function DeclarationDefines a function. Can use the function keyword or simply name the function directly.function myFunc() { echo "Hello, World!"; }
myFunc() { echo "Hello, World!"; }
Calling a FunctionExecutes the function by specifying its name followed by parentheses.myFunc()
Passing ArgumentsFunctions can accept arguments that are passed to them when called. Arguments within functions are accessed via $1, $2, etc.myFunc() { echo "Hello, $1!"; }
myFunc "World"
Return StatementExits the function with an optional return status (0-255). The return status can be captured with $?.myFunc() { return 1; }
echo $?
Local VariablesVariables within a function that are only accessible within that function, declared with local.myFunc() { local localVar="I'm local"; echo $localVar; }
Function OutputFunctions can generate output that can be captured or used by the rest of the script.myFunc() { echo "Function output"; }
RecursionFunctions can call themselves, a technique known as recursion.recursiveFunc() { echo "$1"; if [ $1 -gt 0 ]; then recursiveFunc $(( $1 - 1 )); fi }


Variable Declaration and AssignmentAssigning a value to a named variable.myVar="Hello World"
Environment VariablesPredefined global variables used by the operating system and applications.echo $PATH
Positional ParametersVariables that represent the arguments passed to a script.$1 for the first argument
Special VariablesVariables with specific functions in Bash.$$ for the script’s PID
String VariablesOperations on strings, such as extraction and concatenation.Concatenation: greeting="Hello,"; name="Alice"; echo "$greeting $name"
Numeric VariablesPerforming arithmetic operations.let sum=5+3; echo $sum
ArraysWorking with indexed and associative arrays.Indexed: arr=(one two three); echo ${arr[1]}
Associative: declare -A arr; arr["key"]="value"; echo ${arr["key"]}
Read-only VariablesCreating immutable variables.declare -r myVar="fixed value"
Exporting VariablesMaking variables available to subshell processes.export VAR="value"
Local Variables in FunctionsVariables scoped within a function.function myFunc() { local localVar="I'm local"; }
Variable ExpansionExpanding variables to their values.echo "Your home directory is: $HOME"
Indirect ReferencesAccessing the value of a variable using the name stored in another variable.refVar="myVar"; echo "${!refVar}"

Environment Variables

Environment VariableDescriptionExample Usage
PATHA colon-separated list of directories in which the shell looks for commands.echo $PATH
HOMEThe current user’s home directory.echo $HOME
PWDThe current working directory.echo $PWD
USERThe name of the current user.echo $USER
HOSTNAMEThe name of the computer.echo $HOSTNAME
SHELLThe path to the current user’s shell.echo $SHELL
EDITORThe default file editor to be used.export EDITOR=/usr/bin/vim
LANGThe current locale setting.echo $LANG
HISTSIZEThe number of commands to remember in the command history.echo $HISTSIZE
UIDThe numeric user ID of the current user.echo $UID
TERMThe current terminal emulation.echo $TERM


Arithmetic Operators

OperatorDescriptionSyntax Example
+Additionexpr $a + $b
-Subtractionexpr $a - $b
*Multiplicationexpr $a \* $b
/Divisionexpr $a / $b
%Modulusexpr $a % $b
**Exponentiationexpr $a ** $b

Assignment Operators

OperatorDescriptionSyntax Example
=Assigns value from right side operands to left side operanda=$b
+=Addition assignmenta+=b (equivalent to a=$a+$b)
-=Subtraction assignmenta-=b (equivalent to a=$a-$b)
*=Multiplication assignmenta*=b (equivalent to a=$a*$b)
/=Division assignmenta/=b (equivalent to a=$a/$b)
%=Modulus assignmenta%=b (equivalent to a=$a%$b)

Logical Operators

OperatorDescriptionSyntax Example
&&Logical AND[ $a -lt 20 && $b -gt 100 ]
||Logical OR[ $a -lt 20 || $b -gt 100 ]
!Logical NOT[ !$a -lt 20 ]

Relational Operators

OperatorDescriptionSyntax Example
-eqEqual[ $a -eq $b ]
-neNot equal[ $a -ne $b ]
-gtGreater than[ $a -gt $b ]
-geGreater than or equal to[ $a -ge $b ]
-ltLess than[ $a -lt $b ]
-leLess than or equal to[ $a -le $b ]

Data Structures

Data StructureCharacteristicsExample Usage
ScalarsHold a single value, such as a number or a string.var="Hello World"
Indexed ArraysOrdered collections of values accessed by a numerical index.arr=(apple banana cherry); access with ${arr[1]}
Associative ArraysCollections of key-value pairs accessed by string key. Requires Bash 4+.declare -A arr; arr["key"]="value"; access with ${arr[key]}
StringsA sequence of characters treated as a single value. Extensive manipulation options.str="Bash Scripting"; manipulate with ${str:0:4}
Here DocumentsDirect a block of text into the input of a <<EOF > file.txt
Here StringsDirect a single line string into the input of a command.grep 'pattern' <<< "search in this string"

Statements and Loops

Statement TypeDescriptionExample Usage
if StatementEvaluates a condition and executes a block of code if the condition is true.if [ $a -eq $b ]; then echo "Equal"; fi
elseSpecifies the block of code that is executed if the if condition is false.if [ $a -eq $b ]; then echo "Equal"; else echo "Not Equal"; fi
elifAllows for multiple conditions to be evaluated in an if-else block.if [ $a -eq $b ]; then echo "Equal"; elif [ $a -gt $b ]; then echo "Greater"; else echo "Less"; fi
case StatementSelects a block of code to execute based on the value of a $var in pattern1) command1 ;; pattern2) command2 ;; esac
for LoopIterates over a list of values.for i in {1..5}; do echo "Number $i"; done
while LoopExecutes a block of code as long as a condition is true.while [ $a -le 5 ]; do echo $a; a=$((a+1)); done
until LoopExecutes a block of code until a condition becomes true.until [ $a -gt 5 ]; do echo $a; a=$((a+1)); done
select StatementGenerates a menu from a list and executes code based on user ans in "Choice1" "Choice2"; do case $ans in Choice1) echo "1"; break;; Choice2) echo "2"; break;; esac done
breakExits from a loop.for i in {1..10}; do if [ $i -eq 5 ]; then break; fi; echo $i; done
continueSkips the rest of the loop iteration and proceeds with the next iteration.for i in {1..10}; do if [ $i -eq 5 ]; then continue; fi; echo $i; done

Internal Field Separator (IFS)

ConceptDescriptionExample Usage
IFSSpecial shell variable used to determine how Bash recognizes word boundaries. By default, it is set to space, tab, and newline.N/A
Customizing IFS for Input Field SeparationTemporarily adjusting IFS to split input into fields based on a custom delimiter.IFS=',' read -r first second third <<< "a,b,c"
Restoring Original IFSIt’s good practice to restore IFS to its original state after modifying it to avoid unexpected behavior in script execution.oldIFS="$IFS"; IFS=','; ...; IFS="$oldIFS"
Using IFS in LoopsCustomizing IFS to loop through lines or entries separated by specific delimiters.IFS=$'\n'; for line in $(cat file.txt); do echo "$line"; done
Splitting Strings into ArraysUtilizing IFS to split a string into an array based on a delimiter.IFS=':'; read -ra ADDR <<< "$PATH"; echo "${ADDR[0]}"

String Manipulation

ConcatenationCombining strings.str1="Hello,"; str2="World"; echo "$str1 $str2"
SubstringExtracting parts of a string.${string:position:length}
ReplaceReplacing part of a string.${string/substring/replacement}
LengthGetting the length of a string.${#string}
Uppercase ConversionConverting a string to uppercase.${string^^}
Lowercase ConversionConverting a string to lowercase.${string,,}
Trim WhitespacesTrimming whitespaces from both ends of a string.string=" some text "; trimmed=$(echo -e "$string" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
Split String into ArraySplitting a string into an array using a delimiter.IFS='delimiter' read -ra ADDR <<< "$str"; echo "${ADDR[0]}"
Check if String Contains SubstringChecking if a string contains a specific substring.[[ $str == *"$substring"* ]] && echo "Found" || echo "Not found"
Extracting Strings Using RegexExtracting parts of a string based on a regular expression.[[ $str =~ regex ]] && echo "${BASH_REMATCH[1]}"
Splitting StringsDividing a string into parts based on a delimiter.IFS='delimiter'; read -ra parts <<< "$str"; echo "${parts[0]}"

Regex & Pattern Matching

grepSearches for patterns in files or input.grep 'pattern' filename
echo "text" | grep 'pattern' | Use -E for extended regex, -i for case-insensitive search.
sedStream editor for filtering and transforming text.sed 's/pattern/replacement/' filename | Useful for find-and-replace operations with regex.
awkText processing language that supports regex.awk '/pattern/ { action }' filename | Great for data extraction and reporting.
Pattern ExpansionBash feature for filename matching using *.txt | *, ?, and [abc] are common wildcards.
* (PE)Matches any string of *
? (PE)Matches any single ?.txt
[abc] (PE)Matches any one character in the specified [a-c].txt
Extended GlobbingAdditional pattern matching capabilities in !(exclude).txt | Enable with shopt -s extglob. Patterns like ? and * become available.
? (EG)Matches zero or one occurrence of the ?(a).txt (with shopt -s extglob)
* (EG)Matches zero or more occurrences of the *(a).txt
+ (EG)Matches one or more occurrences of the +(a).txt
@ (EG)Matches exactly one of the @(a).txt
! (EG)Matches anything except the !(a).txt

Date and Time

Command/TechniqueDescriptionExample Usage
dateDisplay or set the system’s date and "+%Y-%m-%d %H:%M:%S"
sleepPauses script execution for a specified time.sleep 5
Arithmetic with dateCalculations with dates to find past or future -d "yesterday"
date -d "+1 week"
Epoch Time (date +%s)Use seconds since the Unix Epoch for +%s
date -d @1609459200
Using date with FilesSet or compare file timestamps.touch -d "1 Jan 2020" file.txt
timedatectl(systemd only) View/set system time and date.timedatectl set-timezone Asia/Tokyo
hwclockAccess or adjust the hardware clock (RTC).hwclock --show
Bash Scripting with printfFormat date and time outputs in scripts.printf "Today is %s.\n" "$(date '+%Y-%m-%d')"

Text Color and Formatting

Code/CommandTypeDescriptionExample Usage
3037Text ColorSet text color (30: Black, …, 37: White)\e[32mGreen Text\e[0m
4047Background ColorSet background color (40: Black, …, 47: White)\e[44mBlue Background\e[0m
0FormattingReset all attributes\e[0mReset
1FormattingBold text\e[1mBold Text\e[0m
2FormattingDim text\e[2mDim Text\e[0m
4FormattingUnderlined text\e[4mUnderlined Text\e[0m
5FormattingBlinking text\e[5mBlinking Text\e[0m (Note: may not work in all terminals)
7FormattingReverse colors\e[7mReversed Colors\e[0m
8FormattingHidden text\e[8mHidden Text\e[0m
tputCommandPortable way to use terminal capabilitiestput setaf 1; echo "Red Text"; tput sgr0
Custom FunctionTechniqueSimplify color usage in scriptsDefine a function: red_text() { echo -e "\e[31m$1\e[0m"; }

Common Errors

ErrorDescription & CauseSolution
Command Not FoundThe system can’t locate the script or command being executed. Caused by a typo, the command not being installed, or a PATH issue.Check for typos, ensure the command is installed, or verify the PATH variable.
Unary Operator ExpectedBash expected a unary operator (like -z or -n), often due to missing quotes around a variable that might become an empty string.Ensure variables are quoted when used in test expressions.
Bad SubstitutionSyntax error, usually because of a typo or misuse of brace expansion or variable substitution features.Check the syntax for brace expansions or variable references. Use ${var} for variables.
sudo: Command Not FoundThe sudo command isn’t installed on the system, common in minimal installations or certain Docker images.Install sudo with the package manager, e.g., apt-get install sudo for Debian/Ubuntu.
Permission DeniedLack of execution permissions on a script or attempt to access a file/directory without the necessary permissions.Use chmod to add execute permission to the script chmod +x or correct file permissions.
No Such File or DirectoryThe script or the file path specified doesn’t exist, possibly due to a wrong path or a typo.Check the path and the filename for typos. Ensure the file exists in the specified location.
Syntax Error Near Unexpected TokenSyntax error caused by incorrect use of Bash syntax, like a misplaced or missing fi for an if statement.Review the script for syntax errors, paying close attention to loops, conditionals, and function definitions.
Integer Expression ExpectedA test command or condition expects an integer, but is given a string or a variable that doesn’t hold an integer.Ensure that variables used in arithmetic tests are integers or quoted if they might be empty.
Too Many ArgumentsA test expression receives more arguments than expected, often due to unquoted variables expanding into multiple words.Quote variables in test expressions to prevent word splitting, especially when a variable could contain spaces.

Script Comments

Single-line CommentUses the # symbol to mark the remainder of the line as a comment.# This is a comment
Multi-line CommentSimulated using : '...' for multiple lines not directly supported.: 'This is a multi-line comment'

Script Output

EchoUses echo command to display a line of text/string.echo "Hello World"
PrintfUses printf for formatted output, similar to the C function.printf "Name: %s\nAge: %d\n" "John" 25
Here DocumentDirects a block of text into an input <<EOF >file.txt\nText\nEOF
RedirectionRedirects output to a file or another command using >, >>, or |.echo "Text" > file.txt

Networking Commands

pingChecks the network connectivity to another
ifconfig / ipDisplays or configures the network interface settings.ip addr show
netstatDisplays network connections, routing tables, and interface statistics.netstat -tuln
curl / wgetDownloads files from the network.curl -O
sshSecurely connects to a remote machine.ssh user@host
scpSecurely copies files between hosts.scp file.txt user@host:/path
nslookup / digQueries DNS to obtain domain name or IP address mapping.dig

Debugging and Error Handling

set -eExits the script if any command returns a non-zero exit status.set -e
set -uTreats unset variables and parameters as an error.set -u
set -xDisplays commands and their arguments as they are executed.set -x
trapCatches or intercepts a signal and executes a specified command.trap 'echo "Error"' ERR
Custom Error MessagesUses conditional statements to check command success and displays custom error messages.command || echo "Command failed"
Debugging ToolsUtilizes external tools like bashdb for debugging Bash scripts.N/A (usage depends on the tool installation)

Bash Sample Script & Syntax

In this section, we’ll walk through a straightforward Bash script that demonstrates how different Bash scripting concepts come together. The following script will provide practical syntax examples, followed by echo statements to showcase what the codeblocks would output. Before the Script, we will instruct you on how to prepare an environment, so that you may run the code yourself!

Setting Up Your Environment

To prepare your environment for running Bash scripts, follow these streamlined steps:

  1. Choose an IDE or Editor:
    Pick your preferred editor like Visual Studio Code, Atom, Sublime Text, or a terminal-based editor like Vim or Emacs for writing Bash scripts.

  2. Ensure Bash is Available:
    Linux/macOS: Bash is typically pre-installed.
    Windows: Install Windows Subsystem for Linux (WSL), Git Bash, or Cygwin.

  3. Create and Open Your Bash Script:
    Open your editor, create a new file, paste the Bash script, and save it with a .sh extension, e.g.,

  4. Set Execute Permissions:
    Open a terminal, navigate to your script’s directory, and run: chmod +x

  5. Run Your Script:
    In the terminal, execute the script by typing: ./ [arg1].
    Note: This script expects an integer as a command-line argument, provide it as such:

./ [integer]

That’s it! You’re ready to execute Bash scripts.

Bash Sample Script

Below, we present a sample Bash script designed to showcase a range of Bash scripting techniques. This example should also provide insight into scripting best practices and how various commands can be used together. Refer to this segment to understand more about how the commands and concepts previously discussed are applied in actual coding scenarios.

# The above line is called a shebang. It tells the system this script should
# be executed using Bash, the Bourne Again SHell, regardless of which shell 
# the user might be using. It specifies the path to the Bash interpreter.

# Variable assignment
echo "Hello, $name"
# Output: Hello, World

# Command-line Parameters

if [ $number -eq 10 ]; then # Compare the parameter to 10
    echo "Your number is equal to 10."
elif [ $number -lt 10 ]; then
    echo "Your number is less than 10."
    echo "Your number is greater than 10."
# Output will depend on number used when calling the script
# If the number "5" was passed, output would be:
# Your number is less than 10.

# printf for formatted output
printf "Name: %s, Age: %d\n" "$name" "$age"
# Output: Name: Alice, Age: 30

# Environment Variables
echo "Your home directory is: $HOME"
# Outputs the path to the user's home directory

# Setting a new environment variable
export MY_VAR="Hello World"
echo $MY_VAR
# Outputs: Hello World

# Using an environment variable in a command
mkdir "$MY_VAR"
# Creates a new directory named "Hello World"

# String Concatenation
concatenatedStr="$str1, $str2!"
echo "Concatenated string: $concatenatedStr"
# Output: Concatenated string: Hello, World!

# String Length
str="Hello World"
echo "Length of '$str': $length"
# Output: Length of 'Hello World': 11

# Substring Extraction
# Extracting 'World' from 'Hello World'
echo "Extracted substring: $extractedStr"
# Output: Extracted substring: World

# Case Conversion
# Converting to uppercase
echo "Uppercase conversion: $upperStr"
# Output: Uppercase conversion: HELLO WORLD

# Replace in String
# Replacing 'World' with 'Bash'
echo "Replaced String: $replacedStr"
# Output: Replaced String: Hello Bash

# Split string based on delimiter (space in this example)
string="one two three"
IFS=' ' read -r -a str_array <<< "$string"
echo "First element after split: ${str_array[0]}" # Outputs "First element after split: one"

# Array usage
fruits=("apple" "banana" "cherry")
echo "First fruit: ${fruits[0]}" # Output: First fruit: apple

# Associative array
declare -A capitals
echo "The capital of France is: ${capitals[France]}" # Output: The capital of France is: Paris

# Reading a file line by line
while IFS= read -r line; do
  echo "Line: $line"
done < myfile.txt # Output: Lines from myfile.txt

# Check if a file exists
if [ -f "$file" ]; then
  echo "$file exists."
  echo "$file does not exist."
fi # Output depends on if myfile.txt exists

# Sorting lines of text in a file alphabetically
echo -e "banana\napple\nfig" > fruits.txt
sort fruits.txt
# This will output:
# apple
# banana
# fig

# Numerical sort with -n flag
echo -e "10\n2\n1" > numbers.txt
sort -n numbers.txt
# This will output:
# 1
# 2
# 10

# Using Here Document to create a configuration file
cat << EOF > myconfig.conf
# My Application Configuration File

echo "Configuration file created with the following content:"
cat myconfig.conf

# Text color and formatting w/ ANSI escape codes
echo -e "\e[31mThis text is red\e[0m" # Red text
echo -e "\e[1;34mThis text is bold and blue\e[0m" # Bold and blue text

echo "Current date and time: $current_date"

# Show date and time one week later
date_one_week_later=$(date -d "$current_date + 1 week") # '-d' flag used for date manipulation, allowing for manipulation like "1 week" to be performed"
echo "Date and time one week later: $date_one_week_later"

# Conditional statement
if [ "$name" = "World" ]; then
  echo "The condition is true."
  echo "The condition is false."
# Output: The condition is true.

# Case statement
case $color in
    echo "Red color selected."
    echo "Blue color selected."
    echo "Unknown color."
esac # Output: Red color selected.

# For Loop
for i in {1..3}; do
  echo "Loop iteration: $i"
# Output: Loop iteration: 1 (then 2, then 3)

# While loop
while [ $count -gt 0 ]; do
  echo "Countdown: $count"
# Output: Countdown: 3 (then 2, then 1)

# Using Break and Continue in Loops
for i in {1..5}; do # Loop through numbers 1 to 5
  if [ "$i" -eq 3 ]; then
    echo "Skipping number $i"
    continue # Skip the rest of the loop at 3 and continue with the next iteration
  if [ "$i" -eq 4 ]; then
    echo "Stopping at number $i"
    break # Exit the loop at number 4
  echo "Number $i" # Will output "Number 1" then "Number 2"

# Use of parameters ($@)
echo "Script parameters: $@"
# When running: ./ param1 param2
# Output: Script parameters: param1 param2

# Assignment Operators and Arithmetic
echo "Initial value of a: $a" # 10

# Assignment
echo "Assigned value of b to a: $a" # Value of b

# Addition assignment
echo "After addition assignment: $a" # 5

# Subtraction assignment
echo "After subtraction assignment: $a" # 2

# Multiplication assignment
echo "After multiplication assignment: $a" # 4

# Logical Operators

# Logical AND
if [[ $a && $b ]]; then
    echo "Both are true."
    echo "a AND b is false."
fi # a AND b is false.

# Logical OR
if [[ $a || $b ]]; then
    echo "At least one is true."
    echo "Both are false."
fi # At least one is true.

# Logical NOT
if [[ ! $b ]]; then
    echo "b is not true."
    echo "b is true."
fi # b is not true.

# Relational Operators

if [ $a -eq $b ]; then
    echo "a is equal to b."
elif [ $a -ne $b ]; then
    echo "a is not equal to b." # This will be executed

if [ $a -lt $b ]; then
    echo "a is less than b." # This will be executed
elif [ $a -gt $b ]; then
    echo "a is greater than b."

if [ $a -le $b ]; then
    echo "a is less than or equal to b." # This will be executed
elif [ $a -ge $b ]; then
    echo "a is greater than or equal to b."

# Simple Regex Matching in Conditional
if [[ "Hello World!" =~ ^Hello ]]; then # Checks if the string starts with "Hello"
  echo "Pattern found in string" # This will be the output
  echo "Pattern not found in string"

# Extracting Parts of String Using Regex
versionString="Version: 5.0.17"
if [[ $versionString =~ Version:\ ([0-9]+)\.([0-9]+)\.([0-9]+) ]]; then
  echo "Major: $majorVersion, Minor: $minorVersion, Patch: $patchVersion" # Major: 5, Minor: 0, Patch: 17

# Pattern Matching with Globbing
echo "Shell script files: ${files[@]}" # Lists .sh files in the current directory

# Using Extended Globbing
shopt -s extglob # Enable extended globbing
echo "Non-shell script files: ${files[@]}" # Lists files not ending with .sh
shopt -u extglob # Disable extended globbing

# Flags and Modifier Flags
# Use 'set -e' to exit the script when any command fails
set -e
echo "Set -e: Exit on first error. Next command will fail, exiting the script early."
cat nonexistentfile.txt
# Use 'set +e' to disable exit on first error
set +e

# Use 'set -u' to treat unset variables as an error
set -u
echo "Set -u: Treating unset variables as an error."
# Use 'set +u' to disable treating unset variables as an error
set +u

# Redirecting stderr to stdout and filtering through grep
ls valid_file.txt non_existent_file.txt 2>&1 | grep "No such file" || echo "One or more files do not exist."

# Using the -f flag with grep to read patterns from a file
echo "Error" > patterns.txt
grep -f patterns.txt log.txt || echo "Pattern not found in log.txt."

# Command chaining with logical AND (&&) and OR (||)
mkdir temp_directory && echo "Directory created successfully." || echo "Failed to create directory."

# Redirecting command output to a file while checking for command success
cp source.txt destination.txt && echo "Copy successful." > operation_status.txt || echo "Copy failed." > operation_status.txt

# Using the exec command with redirection to globally redirect script output
exec 3>&1 4>&2 # Saving file descriptors
exec 1>log_all_output.txt 2>&1 # Redirecting all output to log file
echo "This output will be logged in the file."
exec 1>&3 2>&4 # Restoring file descriptors

# Function definition and call
greet() {
  local person="$1" # Local variable
  echo "Hello, $person"
greet "Alice" # Output: Hello, Alice

# A function to display basic system information and store it into a log file
system_info() {
  echo "System Information Report:"

  # Display current date and time
  echo "Current Date and Time: $(date)"

  # Show system uptime
  echo "System Uptime: $(uptime -p)"

  # Disk space usage
  echo "Disk Space Usage:"
  df -h | grep "^/dev" # Shows disk space usage for devices starting with /dev

  # Memory usage
  echo "Memory Usage:"
  free -h # Display human-readable memory usage

# Calling the system_info function and redirecting output to a file with a timestamp
system_info | tee "system_info_$(date "+%Y-%m-%d_%H-%M-%S").txt"

# File and Directory Manipulation
# Define directory and file names

# Create a new directory
mkdir $dirName

# Navigate into the directory
cd $dirName

# Create a new file and write to it
echo "This is a sample text." > $fileName

# Display the content of the file
echo "Contents of $fileName:"
cat $fileName

# Navigate back to the original directory
cd ..

# Clean up: Remove the directory and its contents
rm -r $dirName

# Error Handling and Conditional Execution
# Attempt to read from a non-existent file and check exit status
cat non_existent_file.txt
if [ $? -ne 0 ]; then
  echo "Failed to read file." # This message is shown if the file doesn't exist.

# Using conditional execution to echo "Error" if a command fails
ls non_existent_directory || echo "Error: Directory does not exist."

# Using && for success execution
echo "Checking for /tmp directory existence..." && ls /tmp || echo "Error: /tmp directory does not exist."

# Explicitly setting an exit status
if [ $? -eq 0 ]; then
  echo "Command succeeded."
  echo "Command failed."

The example code provided above showcased a variety of Bash scripting techniques, from basic command execution to more complex structures like loops and conditionals. Remember, the reference section is there to support you; use it to revisit any concepts covered in the script or to clarify details as you experiment with Bash on your own!


In this article, we’ve equipped you with a handy Bash command reference and demonstrated these concepts through a practical code example. It’s our hope that these tools will bolster your Bash scripting journey, enhancing both your understanding and efficiency. As you move forward, we wish you the best of luck with your future Bash endeavors. Happy scripting!