Print Text
echo "Hello"Prints text to the terminal.
Bash scripting syntax quick reference: variables, tests, loops, functions and redirection, from the GNU Bash manual. Every item below is clearly marked as a shortcut, command, or syntax reference.
echo "Hello"Prints text to the terminal.
name="Ada"Assigns a value (no spaces around =).
if [[ -f file.txt ]]; then
echo "exists"
fiRuns code when a test is true.
#!/bin/bashMakes a script run with Bash.
echo "Hi $name"Expands a variable's value.
for f in *.txt; do
echo "$f"
doneLoops over words or files.
command > out.txt
command >> out.txtWrites (>) or appends (>>) output to a file.
cat log.txt | grep errorSends one command's output into another.
Prints text to the terminal.
echo "Hello"Assigns a value (no spaces around =).
name="Ada"Runs code when a test is true.
if [[ -f file.txt ]]; then
echo "exists"
fiMakes a script run with Bash.
#!/bin/bashExpands a variable's value.
echo "Hi $name"Loops over words or files.
for f in *.txt; do
echo "$f"
doneWrites (>) or appends (>>) output to a file.
command > out.txt
command >> out.txtSends one command's output into another.
cat log.txt | grep errorStores a command's output in a variable.
today=$(date +%F)Reads arguments passed to a script.
$1 $2 # all: "$@" count: $#Chains several conditions.
if [[ $x -gt 0 ]]; then
...
elif [[ $x -eq 0 ]]; then
...
else
...
fiTests for a file or directory.
[[ -f file ]] [[ -d dir ]]Loops over a number range.
for i in {1..10}; do
echo "$i"
doneDefines a reusable function.
greet() {
echo "Hi $1"
}Runs the second command only on success (&&) or failure (||).
make && ./app
cmd || echo "failed"Gives a script permission to run.
chmod +x script.shDoes integer math.
total=$(( a + b ))Reads a line into a variable.
read -p "Name: " nameNumeric test operators.
-eq -ne -lt -le -gt -geChecks if strings are equal or empty.
[[ "$a" == "$b" ]] [[ -z "$a" ]]Repeats while a condition is true.
while [[ $i -lt 5 ]]; do
((i++))
doneLoops over each line of a file.
while IFS= read -r line; do
echo "$line"
done < file.txtCreates an indexed array.
fruits=(apple banana cherry)Sends errors to a file, or errors and output together.
command 2> err.txt
command > all.txt 2>&1Exits the script when any command fails.
set -eUses a default if the variable is unset or empty.
${name:-guest}Matches a value against patterns.
case "$1" in
start) ... ;;
stop) ... ;;
*) echo "usage" ;;
esacLoops over every array element.
for f in "${fruits[@]}"; do echo "$f"; doneGets the exit code of the last command.
echo $?Stops the script with a status code.
exit 1Runs a command without waiting for it.
long_task &Keeps a variable inside a function.
local count=0Gets the number of array elements.
${#fruits[@]}Gets the length of a string.
${#name}Feeds multi-line text into a command.
cat <<EOF
line one
line two
EOFPrints each command before it runs.
set -xIt covers 36 common Bash patterns and examples, including Print Text, Set a Variable, If Statement, Shebang Line, Use a Variable.
Yes. Each reference page includes a copyable code example.
Check the linked documentation for language or runtime version requirements. Libraries, file paths, terminal quoting and environment settings can differ across systems; no blanket platform compatibility is implied.