Linux

bash

GNU Bourne-Again Shell usage guide.

#linux #shell #scripting

History Expansion

Reuse parts of previous commands to speed up your workflow.

CommandDescription
!!Run the last command again.
!nRun the nth command from history.
!$Last argument of the last command.
!^First argument of the last command.
!*All arguments of the last command.
!:nThe nth argument of the last command.
!stringRun the most recent command starting with string.
!?string?Run the most recent command containing string.
^old^newReplace old with new in the previous command and run it.

Brace Expansion

Generate arbitrary strings.

echo filename{,.bak}

Create directory structure.

mkdir -p project/{src,test,doc}/{html,pdf}

Generate ranges.

echo {1..10}

Generate letter ranges.

echo {a..z}

Parameter Expansion

Manipulate variables without external tools like sed or awk.

SyntaxDescription
${var:-default}Use default if var is unset or null.
${var:=default}Set var to default if unset or null.
${var:?error}Exit with error if var is unset or null.
${#var}Length of the variable.
${var:offset:length}Substring.
${var#pattern}Remove shortest match of pattern from front.
${var##pattern}Remove longest match of pattern from front.
${var%pattern}Remove shortest match of pattern from back.
${var%%pattern}Remove longest match of pattern from back.
${var/pattern/replace}Replace first match.
${var//pattern/replace}Replace all matches.
${var^}Uppercase first character.
${var^^}Uppercase all characters.
${var,}Lowercase first character.
${var,,}Lowercase all characters.

Arrays

Indexed Arrays

Declare an indexed array.

arr=(one two three)

Read the first element.

echo "${arr[0]}"

Read all elements.

echo "${arr[@]}"      # All elements

Read the array length.

echo "${#arr[@]}"     # Array length

Read all array indices.

echo "${!arr[@]}"     # All indices

Append an element.

arr+=(four)

Delete one element.

unset "arr[1]"

Associative Arrays (Bash 4.0+)

declare -A map
map[foo]="bar"
map[baz]="qux"

echo "${map[foo]}"

Special Variables

Commonly used special shell variables.

VariableDescription
$0Name of the script.
$1 - $9Arguments passed to the script.
$#Number of arguments passed to the script.
$@All arguments passed to the script.
$*All arguments as a single string.
$?Exit status of the last command.
$$Process ID of the current shell.
$!Process ID of the last background job.
$_Last argument of the previous command.
$-Current set of options.

Conditional Expressions

File Test Operators

OperatorDescription
-e fileFile exists.
-f fileFile is a regular file.
-d fileFile is a directory.
-s fileFile is not empty.
-x fileFile is executable.
-w fileFile is writable.
-r fileFile is readable.
file1 -nt file2file1 is newer than file2.

String & Integer Comparison

OperatorDescription
-z strString is empty (length 0).
-n strString is not empty.
str1 == str2Strings are equal.
str1 != str2Strings are not equal.
int1 -eq int2Integers are equal.
int1 -ne int2Integers are not equal.
int1 -lt int2Less than.
int1 -le int2Less than or equal.
int1 -gt int2Greater than.
int1 -ge int2Greater than or equal.

Advanced Tests ([[ ]])

The [[ ... ]] construct is safer and more powerful than [ ... ].

Regex matching example.

if [[ $str =~ ^[0-9]+$ ]]; then
  echo "Is a number"
fi

Pattern matching example.

if [[ $file == *.tar.gz ]]; then
  echo "Is a tarball"
fi

Logical operator example.

if [[ -f $file && ! -s $file ]]; then
  echo "File exists but is empty"
fi

Loops

For Loop

Iterate over a fixed list.

for item in one two three; do
  echo "$item"
done

Use a C-style loop.

for ((i=0; i<10; i++)); do
  echo "$i"
done

Iterate over a numeric range.

for i in {1..5}; do
  echo "$i"
done

While Loop

count=0
while [[ $count -lt 5 ]]; do
  echo "$count"
  ((count++))
done

Functions

Define reusable code blocks.

Use local to limit a variable to the function scope.

my_function() {
  local var="local variable"
  echo "Argument 1: $1"
  return 0
}

my_function "hello"

💡 Tip: Always use local for variables inside functions to avoid polluting the global scope.


Built-in Commands

Colon (:)

The colon command is a built-in "true" null command that does nothing and always returns success (exit status 0).

Clear a file content without deleting the file:

:> file.txt

Process Substitution

Compare output of two commands.

diff <(ls dir1) <(ls dir2)

Loop over command output.

while read -r line; do echo "$line"; done < <(grep "something" file.txt)

Redirection & Pipes

SyntaxDescription
cmd > fileRedirect stdout to file (overwrite).
cmd >> fileRedirect stdout to file (append).
cmd 2> fileRedirect stderr to file.
cmd &> fileRedirect stdout and stderr to file.
cmd > file 2>&1Same as &>.
cmd < fileRedirect file to stdin.
cmd <<< "str"Here-string (pass string to stdin).
cmd | tee fileOutput to screen and file.

Advanced Redirection

Write a here-document to a file.

cat <<EOF > file.txt
Multi-line
content
EOF

Close file descriptor 3.

exec 3>&-

Open a file for reading on file descriptor 3.

exec 3< file.txt

Keyboard Shortcuts

KeyAction
Ctrl + AGo to beginning of line.
Ctrl + EGo to end of line.
Ctrl + UCut from cursor to beginning of line.
Ctrl + KCut from cursor to end of line.
Ctrl + WCut previous word.
Ctrl + YPaste (yank) what was cut.
Ctrl + RSearch history backward.
Alt + .Insert last argument of previous command.

Safety & Debugging

Strict Mode

Start your scripts with this to fail fast on errors.

set -euo pipefail
IFS=$'\n\t'
OptionDescription
set -eExit immediately if a command exits with a non-zero status.
set -uTreat unset variables as an error.
set -o pipefailReturn value of a pipeline is the value of the last command to exit with a non-zero status.
set -xPrint commands and their arguments as they are executed (debug).

Traps

Execute code when the script exits or receives a signal.

cleanup() {
  rm -f /tmp/tempfile
}

trap cleanup EXIT

Job Control

Manage background processes.

CommandDescription
cmd &Run cmd in background.
jobsList background jobs.
fg %1Bring job 1 to foreground.
bg %1Resume job 1 in background.
disown %1Remove job 1 from shell's job table.
waitWait for all background jobs to finish.

Resources