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/Concept | Description | Syntax 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
Command | Description | Example Usage |
---|
ls | Lists the contents of a directory. | ls -l lists in long format. |
cd | Changes the current directory. | cd /home changes to the /home directory. |
pwd | Prints the current working directory. | pwd displays the current directory path. |
mkdir | Creates a new directory. | mkdir new_dir creates a new directory named new_dir . |
rm | Removes files or directories. | rm file.txt removes file.txt . Use -r for directories. |
echo | Displays a line of text. | echo "Hello World" prints Hello World. |
cat | Concatenates and displays files. | cat file.txt displays the contents of file.txt . |
touch | Creates an empty file or updates the timestamp. | touch new_file.txt creates or updates new_file.txt . |
cp | Copies files and directories. | cp source.txt dest.txt copies source.txt to dest.txt . |
mv | Moves or renames files and directories. | mv old_name.txt new_name.txt renames/moves a file. |
grep | Searches for patterns in files. | grep "search_term" file.txt searches for “search_term” in file.txt . |
find | Searches for files in a directory hierarchy. | find . -name "file.txt" finds all file.txt in the current directory and subdirectories. |
chmod | Changes the file mode (permissions). | chmod 755 script.sh sets script.sh to be readable and executable by everyone, but only writable by the file owner. |
chown | Changes file owner and group. | chown user:group file.txt changes the owner and group of file.txt . |
Command Modifiers
Modifier Flags for ls
Flag | Description | Example Usage |
---|
-l | Lists files in long format, showing detailed information. | ls -l |
-a | Includes directory entries whose names begin with a dot (.). | ls -a |
-h | Human-readable format, with file sizes. | ls -lh |
-r | Reverses the order of the sort. | ls -lr |
-t | Sorts by modification time, newest first. | ls -lt |
Modifier Flags for grep
Flag | Description | Example Usage |
---|
-i | Ignores case distinctions in both the pattern and the input files. | grep -i "pattern" file |
-v | Inverts the match, showing lines that do not match the given pattern. | grep -v "pattern" file |
-c | Counts the number of lines that match the pattern. | grep -c "pattern" file |
-n | Prefixes each line of output with the line number within its input file. | grep -n "pattern" file |
-r | Recursively searches for the pattern in all files under the specified directory. | grep -r "pattern" directory |
Modifier Flags for mkdir
Flag | Description | Example Usage |
---|
-m | Sets the file mode (permissions) of the newly created directory. | mkdir -m 755 newdir |
-p | Creates parent directories as needed and does not return an error if the directory already exists. | mkdir -p /path/to/newdir |
Modifier Flags for rm
Flag | Description | Example Usage |
---|
-r or -R | Removes directories and their contents recursively. | rm -r folderName |
-f | Forces deletion without prompting for confirmation. | rm -f filename |
-i | Prompts for confirmation before deleting each file. | rm -i filename |
Modifier Flags for echo
Flag | Description | Example Usage |
---|
-n | Do not output the trailing newline. | echo -n "text" |
-e | Enable interpretation of backslash escapes. | echo -e "Line 1\nLine 2" |
Modifier Flags for cat
Flag | Description | Example Usage |
---|
-n | Number all output lines. | cat -n file |
-b | Number non-empty output lines. | cat -b file |
-s | Suppress repeated empty output lines. | cat -s file |
Modifier Flags for touch
Flag | Description | Example Usage |
---|
-a | Changes only the access time. | touch -a file |
-m | Changes only the modification time. | touch -m file |
-c | Does not create any files that do not already exist. | touch -c file |
Modifier Flags for cp
Flag | Description | Example Usage |
---|
-r or -R | Recursively copies directories. | cp -r sourceDir targetDir |
-i | Prompts before overwrite. | cp -i sourceFile targetFile |
-v | Verbose mode, shows files as they are copied. | cp -v sourceFile targetFile |
-a | Archives files, copying all file attributes, including permissions and symbolic links. | cp -a sourceDir targetDir |
Modifier Flags for mv
Flag | Description | Example Usage |
---|
-i | Prompts before overwrite. | mv -i oldName newName |
-v | Verbose mode, shows files as they are moved. | mv -v oldLocation newLocation |
-u | Moves only when the source file is newer than the destination file or when the destination file does not exist. | mv -u oldFile newFile |
Modifier Flags for find
Flag | Description | Example Usage |
---|
-name | Search for files by name. | find . -name "filename.txt" |
-type | Search for files of a particular type (e.g., d for directories, f for regular files). | find . -type f |
-perm | Search for files with specific permissions. | find . -perm 644 |
-user | Search for files owned by a specific user. | find . -user username |
-mtime | Search for files modified a certain number of days ago. | find . -mtime +7 |
-exec | Execute a command on each file found. | find . -type f -exec chmod 644 {} \; |
-size | Finds files of a specific size (e.g., +50M for files over 50MB, -50M for files under 50MB). | find . -size +50M |
Modifier Flags for chmod
Flag | Description | Example Usage |
---|
+ | Adds a permission to a file or directory. | chmod +x file |
- | Removes a permission from a file or directory. | chmod -x file |
r | Read permission. | chmod +r file (adds read permission) |
w | Write permission. | chmod +w file (adds write permission) |
x | Execute permission. | chmod +x file (adds execute permission) |
u , g , o | Set permissions for the user (u), group (g), or others (o). | chmod u+x file.txt |
a | Set permissions for all users. | chmod a+w file.txt |
Modifier Flags for chown
Flag | Description | Example Usage |
---|
--recursive or -R | Change file owner and group recursively. | chown -R user:group directory |
--verbose | Output a diagnostic for every file processed. | chown --verbose user file |
--from | Changes 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 |
-c | Like verbose but report only when a change is made. | chown -c new_user file |
-v | Verbose mode. Outputs a diagnostic for every file processed. | chown -v new_user file |
Flags
Flag | Description |
---|
-e | Exit immediately if a command exits with a non-zero status. |
-x | Print commands and their arguments as they are executed. |
-u | Treat unset variables as an error when substituting. |
-o pipefail | Causes a pipeline to return the exit status of the last command in the pipe that failed. |
-v | Print shell input lines as they are read. |
-n | Read commands but do not execute them (syntax check). |
-f | Disable filename expansion (globbing). |
-i | Interactive mode – even if the input is not coming from a terminal. |
-c | Read and execute commands from the following string. |
Command Line Parameters
Parameter | Description |
---|
$0 | The filename of the current script. |
$1 to $9 | The 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. |
$IFS | The Internal Field Separator that is used by the shell to determine how it recognizes fields, or word boundaries, in a string. |
shift | Shifts positional parameters to the left by 1, decreasing the positional parameters count ($# ). Useful for iterating over all arguments. |
set | Used to set or unset options and positional parameters. set -- can be used to overwrite all positional parameters. |
getopts | A utility to parse positional parameters. Used in scripts to process command-line options and flags. |
Sorting Methods
Technique/Command | Description | Example Usage |
---|
sort Command | Sorts 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 Unique | Combines sorting with removing duplicate lines. | sort -u file.txt to sort and remove duplicates. |
Array Sorting | Bash does not natively support array sorting, but you can use external commands or custom functions. | IFS=$'\n' sorted=($(sort <<<"${array[*]}")) |
sort with Reverse Order | Sorts in reverse order. | sort -r file.txt for reverse alphabetical sorting. |
sort with Custom Field | Sorts based on a specific field or column. | sort -k2,2 file.txt to sort by the second column. |
Using awk for Sorting | Preprocessing with awk before sorting, useful for complex data manipulation. | awk '{print $2, $1}' file.txt | sort to swap columns before sorting. |
Numeric Sort | Specifically sorts lines by numerical value. | sort -n file.txt |
Version Sort | Sorts lines by version number, useful for filenames or version tags. | sort -V files.txt for version sorting. |
Data Types
Data Type | Description | Example Usage |
---|
String | Text or characters. Bash does not distinguish between strings and numbers, but strings can contain any character. | str="Hello World" |
Integer | Whole numbers. Bash supports integer operations and comparisons. | int=42; let "int2 = int * 2" |
Array | Indexed or associative collections of values. | Indexed: arr=(one two three) Associative: declare -A assoc_arr=(["key"]="value") |
Command Substitution | Executes a command and substitutes its output. | result=$(ls) |
Arithmetic Expansion | Evaluates an arithmetic expression and substitutes the result. | total=$((3 + 2)) |
File Descriptor | A 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 Document | A type of redirection that feeds a command list or a script a literal block of text. | cat <<EOF
Example text
EOF |
Here String | A form of redirection that feeds a string into the standard input of a command. | grep "search" <<< "search in this string" |
Boolean Operations
Concept | Description | Example Usage |
---|
Boolean Variables | Bash 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 Execution | Use && for AND and || for OR in command sequences. | [[ $a -eq $b ]] && echo "Equal" [[ $a -ne $b ]] || echo "Not equal" |
if Statements | Evaluate 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 Conditions | Advanced test command that allows for pattern matching and more complex expressions. | [[ $a -gt $b && $c -eq 10 ]] |
Exit Statuses | Use 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
Part | Description | Example Usage |
---|
Function Declaration | Defines a function. Can use the function keyword or simply name the function directly. | function myFunc() { echo "Hello, World!"; } myFunc() { echo "Hello, World!"; } |
Calling a Function | Executes the function by specifying its name followed by parentheses. | myFunc() |
Passing Arguments | Functions 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 Statement | Exits the function with an optional return status (0-255). The return status can be captured with $? . | myFunc() { return 1; }
myFunc
echo $? |
Local Variables | Variables within a function that are only accessible within that function, declared with local . | myFunc() { local localVar="I'm local"; echo $localVar; } |
Function Output | Functions can generate output that can be captured or used by the rest of the script. | myFunc() { echo "Function output"; }
result=$(myFunc) |
Recursion | Functions can call themselves, a technique known as recursion. | recursiveFunc() { echo "$1"; if [ $1 -gt 0 ]; then recursiveFunc $(( $1 - 1 )); fi } |
Variables
Concept | Description | Example |
---|
Variable Declaration and Assignment | Assigning a value to a named variable. | myVar="Hello World" |
Environment Variables | Predefined global variables used by the operating system and applications. | echo $PATH |
Positional Parameters | Variables that represent the arguments passed to a script. | $1 for the first argument |
Special Variables | Variables with specific functions in Bash. | $$ for the script’s PID |
String Variables | Operations on strings, such as extraction and concatenation. | Concatenation: greeting="Hello,"; name="Alice"; echo "$greeting $name" |
Numeric Variables | Performing arithmetic operations. | let sum=5+3; echo $sum |
Arrays | Working 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 Variables | Creating immutable variables. | declare -r myVar="fixed value" |
Exporting Variables | Making variables available to subshell processes. | export VAR="value" |
Local Variables in Functions | Variables scoped within a function. | function myFunc() { local localVar="I'm local"; } |
Variable Expansion | Expanding variables to their values. | echo "Your home directory is: $HOME" |
Indirect References | Accessing the value of a variable using the name stored in another variable. | refVar="myVar"; echo "${!refVar}" |
Environment Variables
Environment Variable | Description | Example Usage |
---|
PATH | A colon-separated list of directories in which the shell looks for commands. | echo $PATH |
HOME | The current user’s home directory. | echo $HOME |
PWD | The current working directory. | echo $PWD |
USER | The name of the current user. | echo $USER |
HOSTNAME | The name of the computer. | echo $HOSTNAME |
SHELL | The path to the current user’s shell. | echo $SHELL |
EDITOR | The default file editor to be used. | export EDITOR=/usr/bin/vim |
LANG | The current locale setting. | echo $LANG |
HISTSIZE | The number of commands to remember in the command history. | echo $HISTSIZE |
UID | The numeric user ID of the current user. | echo $UID |
TERM | The current terminal emulation. | echo $TERM |
Operators
Arithmetic Operators
Operator | Description | Syntax Example |
---|
+ | Addition | expr $a + $b |
- | Subtraction | expr $a - $b |
* | Multiplication | expr $a \* $b |
/ | Division | expr $a / $b |
% | Modulus | expr $a % $b |
** | Exponentiation | expr $a ** $b |
Assignment Operators
Operator | Description | Syntax Example |
---|
= | Assigns value from right side operands to left side operand | a=$b |
+= | Addition assignment | a+=b (equivalent to a=$a+$b ) |
-= | Subtraction assignment | a-=b (equivalent to a=$a-$b ) |
*= | Multiplication assignment | a*=b (equivalent to a=$a*$b ) |
/= | Division assignment | a/=b (equivalent to a=$a/$b ) |
%= | Modulus assignment | a%=b (equivalent to a=$a%$b ) |
Logical Operators
Operator | Description | Syntax Example |
---|
&& | Logical AND | [ $a -lt 20 && $b -gt 100 ] |
|| | Logical OR | [ $a -lt 20 || $b -gt 100 ] |
! | Logical NOT | [ !$a -lt 20 ] |
Relational Operators
Operator | Description | Syntax Example |
---|
-eq | Equal | [ $a -eq $b ] |
-ne | Not equal | [ $a -ne $b ] |
-gt | Greater than | [ $a -gt $b ] |
-ge | Greater than or equal to | [ $a -ge $b ] |
-lt | Less than | [ $a -lt $b ] |
-le | Less than or equal to | [ $a -le $b ] |
Data Structures
Data Structure | Characteristics | Example Usage |
---|
Scalars | Hold a single value, such as a number or a string. | var="Hello World" |
Indexed Arrays | Ordered collections of values accessed by a numerical index. | arr=(apple banana cherry) ; access with ${arr[1]} |
Associative Arrays | Collections of key-value pairs accessed by string key. Requires Bash 4+. | declare -A arr; arr["key"]="value" ; access with ${arr[key]} |
Strings | A sequence of characters treated as a single value. Extensive manipulation options. | str="Bash Scripting" ; manipulate with ${str:0:4} |
Here Documents | Direct a block of text into the input of a command. | cat <<EOF > file.txt |
Here Strings | Direct a single line string into the input of a command. | grep 'pattern' <<< "search in this string" |
Statements and Loops
Statement Type | Description | Example Usage |
---|
if Statement | Evaluates a condition and executes a block of code if the condition is true. | if [ $a -eq $b ]; then echo "Equal"; fi |
else | Specifies 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 |
elif | Allows 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 Statement | Selects a block of code to execute based on the value of a variable. | case $var in pattern1) command1 ;; pattern2) command2 ;; esac |
for Loop | Iterates over a list of values. | for i in {1..5}; do echo "Number $i"; done |
while Loop | Executes a block of code as long as a condition is true. | while [ $a -le 5 ]; do echo $a; a=$((a+1)); done |
until Loop | Executes a block of code until a condition becomes true. | until [ $a -gt 5 ]; do echo $a; a=$((a+1)); done |
select Statement | Generates a menu from a list and executes code based on user selection. | select ans in "Choice1" "Choice2"; do case $ans in Choice1) echo "1"; break;; Choice2) echo "2"; break;; esac done |
break | Exits from a loop. | for i in {1..10}; do if [ $i -eq 5 ]; then break; fi; echo $i; done |
continue | Skips 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)
Concept | Description | Example Usage |
---|
IFS | Special 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 Separation | Temporarily adjusting IFS to split input into fields based on a custom delimiter. | IFS=',' read -r first second third <<< "a,b,c" |
Restoring Original IFS | It’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 Loops | Customizing 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 Arrays | Utilizing IFS to split a string into an array based on a delimiter. | IFS=':'; read -ra ADDR <<< "$PATH"; echo "${ADDR[0]}" |
String Manipulation
Method | Description | Example |
---|
Concatenation | Combining strings. | str1="Hello,"; str2="World"; echo "$str1 $str2" |
Substring | Extracting parts of a string. | ${string:position:length} |
Replace | Replacing part of a string. | ${string/substring/replacement} |
Length | Getting the length of a string. | ${#string} |
Uppercase Conversion | Converting a string to uppercase. | ${string^^} |
Lowercase Conversion | Converting a string to lowercase. | ${string,,} |
Trim Whitespaces | Trimming whitespaces from both ends of a string. | string=" some text "; trimmed=$(echo -e "$string" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') |
Split String into Array | Splitting a string into an array using a delimiter. | IFS='delimiter' read -ra ADDR <<< "$str"; echo "${ADDR[0]}" |
Check if String Contains Substring | Checking if a string contains a specific substring. | [[ $str == *"$substring"* ]] && echo "Found" || echo "Not found" |
Extracting Strings Using Regex | Extracting parts of a string based on a regular expression. | [[ $str =~ regex ]] && echo "${BASH_REMATCH[1]}" |
Splitting Strings | Dividing a string into parts based on a delimiter. | IFS='delimiter'; read -ra parts <<< "$str"; echo "${parts[0]}" |
Regex & Pattern Matching
Concept | Description | Example |
---|
grep | Searches for patterns in files or input. | grep 'pattern' filename
echo "text" | grep 'pattern' | Use -E for extended regex, -i for case-insensitive search. |
sed | Stream editor for filtering and transforming text. | sed 's/pattern/replacement/' filename | Useful for find-and-replace operations with regex. |
awk | Text processing language that supports regex. | awk '/pattern/ { action }' filename | Great for data extraction and reporting. |
Pattern Expansion | Bash feature for filename matching using wildcards. | ls *.txt | * , ? , and [abc] are common wildcards. |
* (PE) | Matches any string of characters. | ls * |
? (PE) | Matches any single character. | ls ?.txt |
[abc] (PE) | Matches any one character in the specified set. | ls [a-c].txt |
Extended Globbing | Additional pattern matching capabilities in Bash. | ls !(exclude).txt | Enable with shopt -s extglob . Patterns like ? and * become available. |
? (EG) | Matches zero or one occurrence of the pattern. | ls ?(a).txt (with shopt -s extglob ) |
* (EG) | Matches zero or more occurrences of the pattern. | ls *(a).txt |
+ (EG) | Matches one or more occurrences of the pattern. | ls +(a).txt |
@ (EG) | Matches exactly one of the patterns. | ls @(a).txt |
! (EG) | Matches anything except the pattern. | ls !(a).txt |
Date and Time
Command/Technique | Description | Example Usage |
---|
date | Display or set the system’s date and time. | date "+%Y-%m-%d %H:%M:%S" |
sleep | Pauses script execution for a specified time. | sleep 5 |
Arithmetic with date | Calculations with dates to find past or future dates. | date -d "yesterday"
date -d "+1 week" |
Epoch Time (date +%s ) | Use seconds since the Unix Epoch for calculations. | date +%s
date -d @1609459200 |
Using date with Files | Set 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 |
hwclock | Access or adjust the hardware clock (RTC). | hwclock --show |
Bash Scripting with printf | Format date and time outputs in scripts. | printf "Today is %s.\n" "$(date '+%Y-%m-%d')" |
Text Color and Formatting
Code/Command | Type | Description | Example Usage |
---|
30 –37 | Text Color | Set text color (30: Black, …, 37: White) | \e[32mGreen Text\e[0m |
40 –47 | Background Color | Set background color (40: Black, …, 47: White) | \e[44mBlue Background\e[0m |
0 | Formatting | Reset all attributes | \e[0mReset |
1 | Formatting | Bold text | \e[1mBold Text\e[0m |
2 | Formatting | Dim text | \e[2mDim Text\e[0m |
4 | Formatting | Underlined text | \e[4mUnderlined Text\e[0m |
5 | Formatting | Blinking text | \e[5mBlinking Text\e[0m (Note: may not work in all terminals) |
7 | Formatting | Reverse colors | \e[7mReversed Colors\e[0m |
8 | Formatting | Hidden text | \e[8mHidden Text\e[0m |
tput | Command | Portable way to use terminal capabilities | tput setaf 1; echo "Red Text"; tput sgr0 |
Custom Function | Technique | Simplify color usage in scripts | Define a function: red_text() { echo -e "\e[31m$1\e[0m"; } |
Common Errors
Error | Description & Cause | Solution |
---|
Command Not Found | The 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 Expected | Bash 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 Substitution | Syntax 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 Found | The 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 Denied | Lack 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 script.sh or correct file permissions. |
No Such File or Directory | The 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 Token | Syntax 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 Expected | A 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 Arguments | A 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. |
Topic | Description | Example |
---|
Single-line Comment | Uses the # symbol to mark the remainder of the line as a comment. | # This is a comment |
Multi-line Comment | Simulated using : '...' for multiple lines not directly supported. | : 'This is a multi-line comment' |
Script Output
Topic | Description | Example |
---|
Echo | Uses echo command to display a line of text/string. | echo "Hello World" |
Printf | Uses printf for formatted output, similar to the C function. | printf "Name: %s\nAge: %d\n" "John" 25 |
Here Document | Directs a block of text into an input stream. | cat <<EOF >file.txt\nText\nEOF |
Redirection | Redirects output to a file or another command using > , >> , or | . | echo "Text" > file.txt |
Networking Commands
Debugging and Error Handling
Topic | Description | Example |
---|
set -e | Exits the script if any command returns a non-zero exit status. | set -e |
set -u | Treats unset variables and parameters as an error. | set -u |
set -x | Displays commands and their arguments as they are executed. | set -x |
trap | Catches or intercepts a signal and executes a specified command. | trap 'echo "Error"' ERR |
Custom Error Messages | Uses conditional statements to check command success and displays custom error messages. | command || echo "Command failed" |
Debugging Tools | Utilizes 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:
- 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. Ensure Bash is Available:
Linux/macOS: Bash is typically pre-installed.
Windows: Install Windows Subsystem for Linux (WSL), Git Bash, or Cygwin.
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., myscript.sh.
Set Execute Permissions:
Open a terminal, navigate to your script’s directory, and run: chmod +x myscript.sh.
Run Your Script:
In the terminal, execute the script by typing: ./myscript.sh [arg1].
Note: This script expects an integer as a command-line argument, provide it as such:
./myscript.sh [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.
#!/bin/bash
# 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
name="World"
echo "Hello, $name"
# Output: Hello, World
# Command-line Parameters
number=$1
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."
else
echo "Your number is greater than 10."
fi
# 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
name="Alice"
age=30
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
str1="Hello"
str2="World"
concatenatedStr="$str1, $str2!"
echo "Concatenated string: $concatenatedStr"
# Output: Concatenated string: Hello, World!
# String Length
str="Hello World"
length=${#str}
echo "Length of '$str': $length"
# Output: Length of 'Hello World': 11
# Substring Extraction
# Extracting 'World' from 'Hello World'
extractedStr=${str:6:5}
echo "Extracted substring: $extractedStr"
# Output: Extracted substring: World
# Case Conversion
# Converting to uppercase
upperStr=${str^^}
echo "Uppercase conversion: $upperStr"
# Output: Uppercase conversion: HELLO WORLD
# Replace in String
# Replacing 'World' with 'Bash'
replacedStr=${str/World/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
capitals["France"]="Paris"
capitals["Germany"]="Berlin"
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
file="myfile.txt"
if [ -f "$file" ]; then
echo "$file exists."
else
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
HOST=localhost
PORT=8080
EOF
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
current_date=$(date)
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."
else
echo "The condition is false."
fi
# Output: The condition is true.
# Case statement
color="red"
case $color in
red)
echo "Red color selected."
;;
blue)
echo "Blue color selected."
;;
*)
echo "Unknown color."
;;
esac # Output: Red color selected.
# For Loop
for i in {1..3}; do
echo "Loop iteration: $i"
done
# Output: Loop iteration: 1 (then 2, then 3)
# While loop
count=3
while [ $count -gt 0 ]; do
echo "Countdown: $count"
((count--))
done
# 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
fi
if [ "$i" -eq 4 ]; then
echo "Stopping at number $i"
break # Exit the loop at number 4
fi
echo "Number $i" # Will output "Number 1" then "Number 2"
done
# Use of parameters ($@)
echo "Script parameters: $@"
# When running: ./script.sh param1 param2
# Output: Script parameters: param1 param2
# Assignment Operators and Arithmetic
a=10
b=0
echo "Initial value of a: $a" # 10
# Assignment
a=$b
echo "Assigned value of b to a: $a" # Value of b
# Addition assignment
((a+=5))
echo "After addition assignment: $a" # 5
# Subtraction assignment
((a-=3))
echo "After subtraction assignment: $a" # 2
# Multiplication assignment
((a*=2))
echo "After multiplication assignment: $a" # 4
# Logical Operators
a=true
b=false
# Logical AND
if [[ $a && $b ]]; then
echo "Both are true."
else
echo "a AND b is false."
fi # a AND b is false.
# Logical OR
if [[ $a || $b ]]; then
echo "At least one is true."
else
echo "Both are false."
fi # At least one is true.
# Logical NOT
if [[ ! $b ]]; then
echo "b is not true."
else
echo "b is true."
fi # b is not true.
# Relational Operators
a=10
b=20
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
fi
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."
fi
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."
fi
# 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
else
echo "Pattern not found in string"
fi
# Extracting Parts of String Using Regex
versionString="Version: 5.0.17"
if [[ $versionString =~ Version:\ ([0-9]+)\.([0-9]+)\.([0-9]+) ]]; then
majorVersion=${BASH_REMATCH[1]}
minorVersion=${BASH_REMATCH[2]}
patchVersion=${BASH_REMATCH[3]}
echo "Major: $majorVersion, Minor: $minorVersion, Patch: $patchVersion" # Major: 5, Minor: 0, Patch: 17
fi
# Pattern Matching with Globbing
files=(*.sh)
echo "Shell script files: ${files[@]}" # Lists .sh files in the current directory
# Using Extended Globbing
shopt -s extglob # Enable extended globbing
files=(!(*.sh))
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."
echo $UNSET_VARIABLE
# 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
dirName="exampleDir"
fileName="exampleFile.txt"
# 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.
fi
# 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
false
if [ $? -eq 0 ]; then
echo "Command succeeded."
else
echo "Command failed."
fi
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!
Conclusion
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!