Bash Scripting Cheat Sheet

In Bash

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

CommandDescription
echoPrints text to the terminal
cdChanges the current directory
lsLists the contents of a directory
pwdPrints the current working directory
mkdirCreates a new directory
touchCreates a new file
rmRemoves a file or directory
cpCopies a file or directory
mvMoves a file or directory
catConcatenates and displays files
grepSearches for a pattern in a file
chmodChanges the permissions of a file or directory
chownChanges the owner of a file or directory
sudoExecutes a command with superuser privileges

Variables

SyntaxDescription
variable=valueAssigns a value to a variable
$variableRetrieves the value of a variable
${variable}Alternative syntax for retrieving the value of a variable
unset variableRemoves the value of a variable
readonly variableMakes a variable read-only
export variableMakes a variable available to child processes

Conditional Statements

SyntaxDescription
if condition; then command; fiExecutes a command if a condition is true
if condition; then command; else command; fiExecutes one command if a condition is true, and another command if it is false
if condition; then command; elif condition; then command; else command; fiExecutes 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;; esacExecutes a command based on the value of a variable

Loops

SyntaxDescription
for variable in list; do command; doneExecutes a command for each item in a list
while condition; do command; doneExecutes a command while a condition is true
until condition; do command; doneExecutes a command until a condition is true
select variable in list; do command; doneDisplays a menu of options and executes a command based on the user’s selection

Functions

SyntaxDescription
function_name() { command; }Defines a function
function_name argument1 argument2Calls a function with arguments
return valueReturns a value from a function

Input/Output

SyntaxDescription
command > fileRedirects the output of a command to a file
command >> fileAppends the output of a command to a file
command < fileRedirects the input of a command from a file
command1 | command2Pipes the output of one command to the input of another command
command &> fileRedirects both standard output and standard error to a file

References