Bash Cheat Sheet

mrfdn author

Rafi

Panduan singkat menulis syntax programming menggunakan BASH

Daftar Isi

Pendahuluan

Apa itu Bash?

Bash adalah shell Unix dan bahasa perintah yang ditulis oleh Brian Fox untuk Proyek GNU sebagai pengganti perangkat lunak gratis untuk shell Bourne.

Bash pertama kali dirilis pada tahun 1989, dan sekarang telah digunakan secara luas sebagai shell login default untuk sebagian besar distribusi Linux.

Kenapa Menggunakan Bash?

Bash adalah bahasa program yang solid yang dapat digunakan untuk membuat segalanya berjalan secara otomatis sehingga membuat hidup Anda lebih mudah.

Ini juga merupakan cara yang bagus untuk belajar tentang sistem operasi Linux dan cara kerjanya.

Bagaimana cara menggunakan Bash?

Bash adalah antarmuka baris perintah (CLI) yang digunakan untuk berinteraksi dengan sistem operasi.

Ini digunakan untuk menjalankan perintah dan program pada sistem.

Bash adalah cara yang bagus untuk belajar tentang sistem operasi Linux dan cara kerjanya.


Mari kita mulai

Pertama silahkan membuat sebuah file dengan format .sh kemudian ketikkan beberapa perintah yang akan dijelaskan tiap section di bawah.

Untuk menjalankannya pastikan file tersebut sudah executable dengan melakukan chmod +x filebash.sh.

Kemudian jalankan file bash yang sudah dibuat dengan perintah ./filebash.sh.

Hello World

#!/bin/bash

echo "Hello World"

Execute the script

bash ./hello-world.sh

Variables

#!/bin/bash

# Assign a value to a variable
name="John Doe"

# Print the variable
echo $name

Bash Comments

#!/bin/bash

# This is an inline comment

: '
This is an
multi-line comment
'

Note: Multi-line comments use :’ to open and ’ to close

Arguments

ArgumentDescription
$1 … $9The first 9 arguments to the Bash script. (As mentioned above.)
$0The name of the Bash script.
$#How many arguments were passed to the Bash script.
$$The process ID of the current script.
$*All the arguments
$@All the arguments, starting from the first one.
$-Options passed to the shell.
$?The exit status of the last command that was run.

Example:

#!/bin/bash

# Print the first argument
echo $1

# Print all the arguments
echo $*

# Print all the arguments starting from the first one
echo $@

Execute the script

bash ./arguments.sh "Hello" "World"

Functions

#!/bin/bash

# Define a function
function World() {
  echo "World"
}

# Call the function
echo "Hello $(World)"

Go to Bash Functions

Conditionals

#!/bin/bash

# Define a variable
name="John Doe"

# Check if the variable is empty
if [ -z "$name" ]; then
  echo "The variable is empty"
else
  echo "The variable is not empty"
fi

Go to Bash Conditionals

Brace expansion

ExpressionDescription
{a,b,c}Matches a, b, or c.
{a,b,c}.tsMatches a.ts, b.ts, or c.ts.
{1..5}Matches any number from 1 to 5.

Example:

#!/bin/bash

# Create a directory a, b, c
mkdir {a,b,c}

# Create a file with the name a.ts, b.ts, c.ts
touch {a,b,c}.ts

# Create a file with the name 1.txt, 2.txt, 3.txt, 4.txt, 5.txt
touch {1..5}.txt

Shell execution

#!/bin/bash

# Execute a command
echo "Hello $(whoami)"

# Execute a command with ``
echo "Hello `whoami`"

Bash Parameter expansions

Syntax

ExpressionDescription
${FOO%suffix}Remove the shortest matching suffix pattern from the end of $FOO.
${FOO#prefix}Remove the shortest matching prefix pattern from the beginning of $FOO.
${FOO%%suffix}Remove the longest matching suffix pattern from the end of $FOO.
${FOO##prefix}Remove the longest matching prefix pattern from the beginning of $FOO.
${FOO/from/to}Replace the first matching instance of from with to in $FOO.
${FOO//from/to}Replace all matching instances of from with to in $FOO.
${FOO/%from/to}Replace the suffix matching from with to in $FOO.
${FOO/#from/to}Replace the prefix matching from with to in $FOO.
Substrings
${FOO:0:3}Extract the first 3 characters of $FOO.
${FOO:(-3):3}Extract the last 3 characters of $FOO.
Length
${#FOO}The length of $FOO.
Default values
${FOO:-bar}Use the value of $FOO if it is set, otherwise use bar.
${FOO:=bar}Use the value of $FOO if it is set, otherwise set it to bar.
${FOO:+bar}Use bar if $FOO is set, otherwise use the empty string.
${FOO:?bar}Use the value of $FOO if it is set, otherwise print bar to stderr.

Substitution

# !/bin/bash

echo ${FOO:-val} # Use the value of $FOO if it is set, otherwise use val.

STR="/path/to/file.txt"

# Remove the extension
echo ${STR%.txt} # /path/to/file

# Change the extension
echo ${STR%.txt}.md # /path/to/file.md

# Get the directory
echo ${STR%/*} # /path/to

# Get the extension
echo ${STR##*.} # txt

# Get the file
echo ${STR##*/} # file.txt

# Change the file name to test
echo ${STR/file/test} # /path/to/test.txt

Slicing

#!/bin/bash

STR="Hello World"

# Get the first 5 characters
echo ${STR:0:5} # Hello

# Get the last 5 characters
echo ${STR:(-5):5} # World

Basepath & Dirpath

#!/bin/bash

STR="/path/to/file.txt"

# Get the basepath
echo ${STR##*/} # file.txt

# Get the dirpath
echo ${STR%/*} # /path/to

Transform

#!/bin/bash

STR="Hello World"

# Convert to uppercase
echo ${STR^^} # HELLO WORLD

# Convert to lowercase
echo ${STR,,} # hello world

# Capitalize the first letter
echo ${STR^} # Hello world

# Capitalize the first letter of each word
echo ${STR^^^} # Hello World

# Capitalize the first letter of each word
echo ${STR^ ^} # Hello World

# Capitalize the first letter of each word
echo "${STR[@]^}" # Hello World

# lowercase the first letter of each word
echo "${STR[@],}" # hello world

Bash Arrays

Defining arrays

#!/bin/bash

# Define an array
NUMBERS=(1 2 3)

# Define an array with a range
array[0]=1
array[1]=2
array[2]=3

# Define an array with a range
array=({1..3})

# Define an array1, array2, and array3
array1=(1 2 3)
array2=(4 5 6)
array3=(7 8 9)

# Merge the arrays
array=("${array1[@]}" "${array2[@]}" "${array3[@]}")

# Declare an array with a range
declare -a numbers(1 2 3)

# Add an element to the array in the last position
numbers+=4

# Add an element to the array in the first position
numbers=(0 "${numbers[@]}")

# Add an element to the array in the second position
numbers=("${numbers[@]:0:1}" "1" "${numbers[@]:1}")

Indexing arrays

ExpressionDescription
${NUMBERS[0]}Get the first element of the array.
${NUMBERS[-1]}Get the last element of the array.
${NUMBERS[@]}Get all elements of the array.
${NUMBERS[*]}Get all elements of the array.
${#NUMBERS[@]}Get the length of the array.
${#NUMBERS}Get the length of the 1st element of the array.
${#NUMBERS[n]}Get the length of the nth element of the array.
${NUMBERS[@]:3:2}Get the 2 elements starting at index 3.
${!NUMBERS[@]}Get all indexes of the array.

Iterating arrays

#!/bin/bash

# Define an array
NUMBERS=(1 2 3)

# Iterate over the array
for i in "${NUMBERS[@]}"; do
  echo $i
done

# Iterate over the array with index

for i in "${!NUMBERS[@]}"; do
  echo "$i: ${NUMBERS[$i]}"
done

Bash Arrays Operations

#!/bin/bash

# Define an array
NAMES=("John" "Jane" "Jack")

# Push an element to the array
NAMES=("${NAMES[@]}" "Jill")

# Other way to push an element to the array
NAMES+=("Jill")

# Remove by RegEx pattern matching
NAMES=("${NAMES[@]/*ll*/}")

# Remove one element by index
unset NAMES[0]

# Duplicate an array
NAMES2=("${NAMES[@]}")

# Concatenate two arrays
NAMES3=("${NAMES[@]}" "${NAMES2[@]}")

# Reverse an array
NAMES=("${NAMES[@]}" | tac)

# Sort an array
NAMES=($(printf '%s\n' "${NAMES[@]}" | sort))

# Read from file
NAMES=($(cat file.txt))

Arrays as arguments

#!/bin/bash

# Define an array
NAMES=("John" "Jane" "Jack")

# Pass the array as arguments
function print_names() {
  for i in "${@}"; do
    echo $i
  done
}

print_names "${NAMES[@]}"

Bash Dictionaries

Defining dictionaries

#!/bin/bash

# Define a dictionary
declare -A DICT

# Add dictionary with values
DICT["key1"]="value1"
DICT["key2"]="value2"
DICT["key3"]="value3"

Working with dictionaries

#!/bin/bash

# Pritn by key
echo ${DICT["key1"]}

# Print all values
echo ${DICT[@]}

# Print all keys
echo ${!DICT[@]}

# Length of the dictionary
echo ${#DICT[@]}

# Delete a key2
unset DICT["key2"]

Iteration over dictionaries

#!/bin/bash

# Iterate over the dictionary
for val in "${DICT[@]}"; do
  echo $val
done

# Iterate over the keys
for key in "${!DICT[@]}"; do
  echo $key
done

# Iterate over the dictionary with keys
for key in "${!DICT[@]}"; do
  echo "$key: ${DICT[$key]}"
done

Bash Conditionals

Integer conditions

OperatorDescription
[[ NUM -eq NUM ]]Equal
[[ NUM -ne NUM ]]Not equal
[[ NUM -gt NUM ]]Greater than
[[ NUM -ge NUM ]]Greater than or equal
[[ NUM -lt NUM ]]Less than
[[ NUM -le NUM ]]Less than or equal
(( NUM > NUM ))Greater than
(( NUM >= NUM ))Greater than or equal
(( NUM < NUM ))Less than
(( NUM <= NUM ))Less than or equal

String conditions

OperatorDescription
[[ -z STR ]]Empty string
[[ -n STR ]]Not empty
[[ STR ]]Not empty
[[ STR == STR ]]Equal
[[ STR = STR ]]Equal (deprecated)
[[ STR < STR ]]Less than
[[ STR > STR ]]Greater than
[[ STR =~ REGEX ]]Match regex
[[ STR =~ ^REGEX$ ]]Match regex (full match)

File conditions

OperatorDescription
[[ -f FILE ]]File exists
[[ -e FILE ]]File exists
[[ -d FILE ]]Directory exists
[[ -r FILE ]]File is readable
[[ -h FILE ]]File is a symbolic link
[[ -w FILE ]]File is writable
[[ -x FILE ]]File is executable
[[ -s FILE ]]File is not empty
[[ -O FILE ]]File is owned by you
[[ -G FILE ]]File is owned by group
[[ -L FILE ]]File is a symbolic link
[[ FILE1 -nt FILE2 ]]File1 is newer than File2
[[ FILE1 -ot FILE2 ]]File1 is older than File2
[[ FILE1 -ef FILE2 ]]File1 is same as File2

More conditions

OperatorDescription
[[ -o OPTION ]]Option is enabled
[[ -v VAR ]]Variable is set
[[ -R VAR ]]Variable is read-only
[[ -S VAR ]]Variable is a socket
[[ -p VAR ]]Variable is a pipe
[[ -b VAR ]]Variable is a block dev
[[ ! CONDITION ]]Negate condition
[[ CONDITION1 && CONDITION2 ]]AND
[[ CONDITION1 || CONDITION2 ]]OR

Example:

#!/bin/bash

# String
if [[ -z $STR ]]; then
  echo "Empty string"
elif [[ -n $STR ]]; then
  echo "Not empty"
else
  echo "Not empty"
fi

# Combinations
if [[ $X && $Y ]]; then
  echo "Both are true"
elif [[ $X || $Y ]]; then
  echo "One is true"
else
  echo "Both are false"
fi

# Regex
if [[ $STR =~ ^[0-9]+$ ]]; then
  echo "String is a number"
fi

# Smaller
if [[ $X < $Y ]]; then
  echo "X is smaller than Y"
fi

# Exists
if [[ -f $FILE ]]; then
  echo "File exists"
fi

Logical and, or, not

#!/bin/bash

# Logical and
if [[ $X && $Y ]]; then
  echo "Both are true"
fi

# Logical or
if [[ $X || $Y ]]; then
  echo "One is true"
fi

# Logical not
if [[ ! $X ]]; then
  echo "X is false"
fi

Bash Loops

For loop

#!/bin/bash

# For loop
for i in {1..10}; do
  echo $i
done

For loop like C

#!/bin/bash

# For loop like C
for ((i=1; i<=10; i++)); do
  echo $i
done

Ranges

#!/bin/bash

# Ranges
for i in {1..10}; do
  echo $i
done

# Ranges with step
for i in {1..10..2}; do
  echo $i
done

While loop

#!/bin/bash

# While loop with auto increment
i=1
while [[ $i -le 10 ]]; do
  echo $i
  ((i++))
done

# While loop with auto decrement
i=10
while [[ $i -ge 1 ]]; do
  echo $i
  ((i--))
done

Continue

#!/bin/bash

# Continue
for i in {1..10}; do
  if [[ $i -eq 5 ]]; then
    continue
  fi
  echo $i
done

Break

#!/bin/bash

# Break
for i in {1..10}; do
  if [[ $i -eq 5 ]]; then
    break
  fi
  echo $i
done

Until loop

#!/bin/bash

# Until loop
i=1
until [[ $i -gt 10 ]]; do
  echo $i
  ((i++))
done

Infinite loop

#!/bin/bash

# Infinite loop
while true; do
  echo "Infinite loop"
done

# Infinite loop with (shorthand)
while :; do
  echo "Infinite loop"
done

Loop over file.txt Read line by line

#!/bin/bash

# Loop over file.txt
while read -r line; do
  echo $line
done < file.txt

Bash Functions

Defining functions

#!/bin/bash

# Defining functions
function hello() {
  echo "Hello World"
}

# Defining functions (shorthand), same as above
hello() {
  echo "Hello World"
}

# Calling functions
hello

Returning values

#!/bin/bash

# Returning values
function myfunc() {
  local myresult='Hello World'
  echo $myresult
}

# Calling functions
result="$(myfunc)"

# Printing result
echo $result

Raising errors

#!/bin/bash

# Raising errors
function myfunc() {
  local myresult='Hello World'
  echo $myresult
  return 1
}

# Calling functions
if myfunc; then
  echo "Success"
else
  echo "Failed"
fi

Passing arguments

#!/bin/bash

# Passing arguments
function myfunc() {
  echo "First argument: $1"
  echo "Second argument: $2"
  echo "All arguments: $@"
}

# Calling functions
myfunc "Hello" "World"

Bash Options

Options

OptionDescription
-oSet options
-o noclobberDisable file overwriting
-o errexitExit on error
-o errtraceExit on error in function
-o functraceExit on error in function
-o nounsetExit on undefined variable
-o pipefailExit on pipe error
-o xtracePrint commands and their arguments as they are executed
-o verbosePrint shell input lines as they are read

Example:

#!/bin/bash

# Options

# Disable file overwriting
set -o noclobber

# Exit on error
set -o errexit

# Exit on error in function
set -o errtrace

# Exit on error in function
set -o functrace

# Exit on undefined variable
set -o nounset

# Exit on pipe error
set -o pipefail

# Print commands and their arguments as they are executed
set -o xtrace

# Print shell input lines as they are read
set -o verbose

Glob options

OptionDescription
shoptSet options
shopt -s nullglobDisable globbing and return an empty string if no matches are found
shopt -s failglobDisable globbing and return an empty string if no matches are found
shopt -s dotglobInclude hidden files in globbing
shopt -s nocaseglobCase insensitive globbing
shopt -s globstarEnable recursive globbing
shopt -s globasciirangesEnable globbing of ASCII character ranges
shopt -s extglobEnable extended globbing

Example:

#!/bin/bash

# Glob options

# Disable globbing and return an empty string if no matches are found
shopt -s nullglob

# Disable globbing and return an empty string if no matches are found
shopt -s failglob

# Include hidden files in globbing
shopt -s dotglob

# Case insensitive globbing
shopt -s nocaseglob

# Enable recursive globbing
shopt -s globstar

# Enable globbing of ASCII character ranges
shopt -s globasciiranges

# Enable extended globbing
shopt -s extglob

Bash History

Commands

CommandDescription
historyList history
sudo !!Run last command as root
shopt -s histappendAppend to the history file, don’t overwrite it

Expansions

ExpansionDescription
!$Last argument of last command
!*All arguments of last command
!^First argument of last command
!-nCommand number n
!-n:pPrint command number n
!-n:eEdit command number n in $EDITOR
!nCommand starting with n
!<command>Last command starting with command
!?command?Last command containing command

Bash History Operations

OperationDescription
!!Execute last command
!!:s/foo/barSubstitute foo with bar in last command
!!:gs/foo/barSubstitute foo with bar globally in last command
!$:tPrint last argument of last command
!$:hPrint last argument of last command without extension

Note: !! is the same as !-1, and !! & !$ can be replaced with any valid expansion.

Slices

SliceDescription
!!:nLast n arguments of last command
!!:n-mArguments n to m of last command
!!:n-$Arguments n to end of last command
!!:0-nArguments 0 to n of last command
!^First argument of last command
!^:nFirst n characters of first argument
!^:n-mCharacters n to m of first argument
!$Last argument of last command
!$:nLast n characters of last argument
!$:n-mCharacters n to m of last argument

Note: !!:n is the same as !-1:n, and !! can be replaced with any valid expansion i.e. !cat, !-2, !42, etc.


Others

Numeric calculations

#!/bin/bash

# Numeric calculations
echo $(( 1 + 1 ))
echo $(( 1 - 1 ))
echo $(( 1 * 1 ))
echo $(( 1 / 1 ))

# Floating point calculations
echo "scale=2; 1 / 3" | bc

# Random number between 0 and 100
echo $(( RANDOM % 100 ))

printf

#!/bin/bash

# printf
printf "%s\n" "Hello World"

# printf with colors
printf "\033[0;31m%s\033[0m\n" "Hello World"

Subshells

#!/bin/bash

# Subshells
echo $(date)

# Subshells with variables
date=$(date)

# Change directory in subshell
(cd /tmp && echo "Current directory is: $(pwd)")

Inspecting commands

#!/bin/bash

# Inspecting commands
command -v ls

# Inspecting commands with arguments
command -v ls -- --help

Redirection

#!/bin/bash

# Redirection

# Redirect stdout to file
echo "Hello World" > /tmp/hello.txt

# Redirect stdout
node test.js > /tmp/test.txt

# Append stdout
node test.js >> /tmp/test.txt

# Redirect stderr
node test.js 2> /tmp/test.txt

# Append stderr
node test.js 2>> /tmp/test.txt

# Redirect stdout and stderr
node test.js &> /tmp/test.txt

# Append stdout and stderr
node test.js &>> /tmp/test.txt

# Redirect stdout and stderr
node test.js > /tmp/test.txt 2>&1

# Append stdout and stderr
node test.js > /tmp/test.txt 2>&1

Pipes

#!/bin/bash

# Pipes

# Pipe stdout to stdin
echo "Hello World" | cat

# Pipe stdout and stderr to stdin
node test.js | cat

# Pipe stdout to stdin and stderr to stdout
node test.js 2>&1 | cat

Process substitution

#!/bin/bash

# Process substitution

# Redirect stdout to file
echo "Hello World" > >(cat > /tmp/hello.txt)

# Redirect stdout and stderr to file
node test.js > >(cat > /tmp/test.txt) 2>&1

Source relative

#!/bin/bash

# Source relative
source "$(dirname "$0")/test.sh"

Directory of script

#!/bin/bash

# Directory of script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

Switch/Case

#!/bin/bash

# Switch/Case
case "$1" in
  start)
    echo "Starting..."
    ;;
  stop)
    echo "Stopping..."
    ;;
  *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac

Trap errors

#!/bin/bash

# Trap errors
trap 'echo "Error on line $LINENO"' ERR

# trap 'trapecho "$BASH_COMMAND"' ERR
trapecho() {
  echo "ERROR: ${BASH_SOURCE[1]}:${BASH_LINENO[0]}: ${FUNCNAME[1]}: $1"
}

# trace ERR through 'time command' and other functions
set -o errtrace

# trace ERR through 'time command' and other functions
trap 'trapecho "$BASH_COMMAND"' ERR

Getting options

#!/bin/bash

# Getting options
while getopts ":a:b:" opt; do
  case $opt in
    a) echo "-a was triggered, Parameter: $OPTARG" >&2
    ;;
    b) echo "-b was triggered, Parameter: $OPTARG" >&2
    ;;
    \?) echo "Invalid option: -$OPTARG" >&2
    ;;
  esac
done

Check for command’s result

#!/bin/bash

# Check for command's result
if command -v ls &> /dev/null; then
  echo "ls exists"
else
  echo "ls does not exist"
fi

Grep check

#!/bin/bash

# Grep check
if grep -q "Hello World" /tmp/hello.txt; then
  echo "Hello World exists"
else
  echo "Hello World does not exist"
fi

# Check for file
if [ -f /tmp/hello.txt ]; then
  echo "/tmp/hello.txt exists"
else
  echo "/tmp/hello.txt does not exist"
fi

Special variables

VariableDescription
$0Script name
$?Exit code
$!PID of last background process
$$PID of current process

Backslash escapes

EscapeDescription
\aAlert (bell)
\bBackspace
\eEscape
\fForm feed
\nNew line
\rCarriage return
\tHorizontal tab
\vVertical tab
\\Backslash
\nnnOctal number
\xhhHexadecimal number
&Background process
&&Run next command if previous command was successful
|Pipe
| |Run next command if previous command was unsuccessful

Note: The backslash escape is not needed for the following characters: ! $ " ' \, and \ can be used to escape itself and the following characters.

Heredoc

#!/bin/bash

# Heredoc
cat << EOF
Hello World
EOF

Reading input

#!/bin/bash

# Reading input
read -p "Enter your name: " name
echo "Hello $name"

Optional arguments

#!/bin/bash

# Optional arguments
if [ -z "$1" ]; then
  echo "No arguments supplied"
else
  echo "Argument supplied: $1"
fi

Strict mode

#!/bin/bash

# Strict mode
set -euo pipefail
IFS=$'\n\t'

Resources

mrfdn author

Rafi

  • 15 year+ of Linux user.
  • 5 years+ blogger and web developer.

Jika artikel yang dibuatnya ternyata bermanfaat, support dengan cara

Baca juga

comments powered by Disqus