| Shebang |
#!/bin/bash |
Defines the script interpreter. |
| Variable assignment |
VAR=value |
Assign a value to a variable. |
| Print to console |
echo 'Hello World' |
Display text output. |
| Read input |
read var |
Read user input into variable. |
| If statement |
if [ condition ]; then ... fi |
Conditional execution. |
| For loop |
for i in {1..5}; do ... done |
Loop through a sequence. |
| While loop |
while [ condition ]; do ... done |
Loop while condition is true. |
| Function definition |
function_name() { commands; } |
Define reusable code block. |
| Exit script |
exit 0 |
Exit script with status code. |
| Command substitution |
VAR=$(command) |
Capture command output. |
| Test file existence |
[ -f filename ] |
Check if file exists. |
| Check string equality |
[ "$var" == "value" ] |
String comparison. |
| Arrays |
arr=(val1 val2 val3) |
Define an array. |
| Access array element |
${arr[0]} |
Get element at index. |
| Redirect output |
command > file.txt |
Overwrite file with command output. |
| Append output |
command >> file.txt |
Append command output to file. |
| Pipe commands |
command1 | command2 |
Send output of one command to another. |
| Background process |
command & |
Run command in the background. |
| Get script arguments |
$1, $2, ... |
Access positional parameters. |