Bash scripting is a powerful tool that allows users to automate tasks and perform complex operations on their computer systems. Bash is a command-line shell that is widely used in Linux and Unix-based operating systems. It provides a powerful set of tools for managing files, running programs, and manipulating data.
Bash scripts are essentially a series of commands that are executed in sequence. They can be used to automate repetitive tasks, such as backing up files or running system maintenance tasks. They can also be used to perform complex operations, such as parsing data or manipulating files.
This cheat sheet provides an extensive list of commands and syntax used in Bash scripting. Enjoy!
Basic Commands
Command
Description
echo
Prints text to the terminal
cd
Changes the current directory
ls
Lists the contents of a directory
pwd
Prints the current working directory
mkdir
Creates a new directory
touch
Creates a new file
rm
Removes a file or directory
cp
Copies a file or directory
mv
Moves a file or directory
cat
Concatenates and displays files
grep
Searches for a pattern in a file
chmod
Changes the permissions of a file or directory
chown
Changes the owner of a file or directory
sudo
Executes a command with superuser privileges
Variables
Syntax
Description
variable=value
Assigns a value to a variable
$variable
Retrieves the value of a variable
${variable}
Alternative syntax for retrieving the value of a variable
unset variable
Removes the value of a variable
readonly variable
Makes a variable read-only
export variable
Makes a variable available to child processes
Conditional Statements
Syntax
Description
if condition; then command; fi
Executes a command if a condition is true
if condition; then command; else command; fi
Executes one command if a condition is true, and another command if it is false
if condition; then command; elif condition; then command; else command; fi
Executes one command if the first condition is true, another command if the second condition is true, and another command if both conditions are false
case variable in pattern1) command;; pattern2) command;; *) command;; esac
Executes a command based on the value of a variable
Loops
Syntax
Description
for variable in list; do command; done
Executes a command for each item in a list
while condition; do command; done
Executes a command while a condition is true
until condition; do command; done
Executes a command until a condition is true
select variable in list; do command; done
Displays a menu of options and executes a command based on the user’s selection
Functions
Syntax
Description
function_name() { command; }
Defines a function
function_name argument1 argument2
Calls a function with arguments
return value
Returns a value from a function
Input/Output
Syntax
Description
command > file
Redirects the output of a command to a file
command >> file
Appends the output of a command to a file
command < file
Redirects the input of a command from a file
command1 | command2
Pipes the output of one command to the input of another command
command &> file
Redirects both standard output and standard error to a file