Bash Basics
#!/bin/bash
: Shebang to specify the shell used for the scriptecho "text"
: Print a message to the terminalls
: List the contents of the current directorycd /path/to/directory
: Change to a different directorypwd
: Print the current working directory
Variables
variable="value"
: Assign a value to a variableecho $variable
: Print the value of a variable$1
,$2
, etc.: Command line arguments$#
: Number of command line arguments$@
: All command line arguments$?
: Exit status of the last command
Conditionals
if [ condition ]; then ... fi
: If statementelse
: Else statementelif
: Else if statement-eq
,-ne
,-lt
,-le
,-gt
,-ge
: Comparison operators&&
,||
: Logical operators
Loops
for variable in list; do ... done
: For loopwhile [ condition ]; do ... done
: While loop
Files
touch file.txt
: Create an empty filecat file.txt
: Display the contents of a fileecho "text" > file.txt
: Overwrite a file with textecho "text" >> file.txt
: Append text to a filecp source destination
: Copy a filemv source destination
: Move or rename a filerm file.txt
: Remove a file
Miscellaneous
sleep n
: Pause for n secondschmod +x script.sh
: Make a script executablesource script.sh
or./script.sh
: Execute a script
Note: This cheat sheet is intended as a quick reference and does not cover all aspects of bash scripting. It is important to consult the manual pages or other resources for more information and guidance.