> ## Documentation Index
> Fetch the complete documentation index at: https://docs.momokomizumi.me/llms.txt
> Use this file to discover all available pages before exploring further.

# Bash Scripting Cheatsheet 

> Bash Cheatsheet for scripting

### Example

```bash theme={null}
#!/usr/bin/env bash

name="John"
echo "Hello $name!"
```

### Variables

```bash theme={null}
name="John"
echo $name  # see below
echo "$name"
echo "${name}!"
```

<Note>
  Generally quote your variables unless they contain wildcards to expand or command fragments.
</Note>

```bash theme={null}
wildcard="*.txt"
options="iv"
cp -$options $wildcard /tmp
```

### String quotes

```bash theme={null}
name="John"
echo "Hi $name"  #=> Hi John
echo 'Hi $name'  #=> Hi $name
```

### Shell execution

```bash theme={null}
echo "I'm in $(pwd)"
echo "I'm in `pwd`"  # obsolescent
# Same
```

> See [Command substitution](https://web.archive.org/web/20230326081741/https://wiki.bash-hackers.org/syntax/expansion/cmdsubst)

### Conditional execution

```bash theme={null}
git commit && git push
git commit || echo "Commit failed"
```

### Functions

```bash theme={null}
get_name() {
  echo "John"
}

echo "You are $(get_name)"
```

> See: [Functions](https://devhints.io/bash#functions)

### Conditionals

```bash theme={null}
if [[ -z "$string" ]]; then
  echo "String is empty"
elif [[ -n "$string" ]]; then
  echo "String is not empty"
fi
```

> See: [Conditionals](https://devhints.io/bash#conditionals)

### Strict mode

```bash theme={null}
set -euo pipefail
IFS=$'\n\t'
```

> See: [Unofficial bash strict mode](http://redsymbol.net/articles/unofficial-bash-strict-mode/)

### Brace expansion

<Frame>
  Lets take `echo {A,B}.js` as an example:

  | `{A,B}`           | Same as `A B`                                         |
  | :---------------- | :---------------------------------------------------- |
  | `{A,B}.js`        | Same as `A.js B.js`                                   |
  | `{1..5}`          | Same as `1 2 3 4 5`                                   |
  | `{{1..3},{7..9}}` | Same as `1 2 3 7 8 9`ge of your Frame component here. |
</Frame>

> See: [Brace expansion](https://web.archive.org/web/20230207192110/https://wiki.bash-hackers.org/syntax/expansion/brace)

## Parameter expansions

***

### Basics

```bash theme={null}
name="John"
echo "${name}"
echo "${name/J/j}"    #=> "john" (substitution)
echo "${name:0:2}"    #=> "Jo" (slicing)
echo "${name::2}"     #=> "Jo" (slicing)
echo "${name::-1}"    #=> "Joh" (slicing)
echo "${name:(-1)}"   #=> "n" (slicing from right)
echo "${name:(-2):1}" #=> "h" (slicing from right)
echo "${food:-Cake}"  #=> $food or "Cake"
```

```bash theme={null}
length=2
echo "${name:0:length}"  #=> "Jo"
```

> See: [Parameter expansion](https://web.archive.org/web/20230408142504/https://wiki.bash-hackers.org/syntax/pe)

```bash theme={null}
str="/path/to/foo.cpp"
echo "${str%.cpp}"    # /path/to/foo
echo "${str%.cpp}.o"  # /path/to/foo.o
echo "${str%/*}"      # /path/to

echo "${str##*.}"     # cpp (extension)
echo "${str##*/}"     # foo.cpp (basepath)

echo "${str#*/}"      # path/to/foo.cpp
echo "${str##*/}"     # foo.cpp

echo "${str/foo/bar}" # /path/to/bar.cpp
```

```bash theme={null}
str="Hello world"
echo "${str:6:5}"    # "world"
echo "${str: -5:5}"  # "world"
```

```bash theme={null}
src="/path/to/foo.cpp"
base=${src##*/}   #=> "foo.cpp" (basepath)
dir=${src%$base}  #=> "/path/to/" (dirpath)
```

### Prefix name expansion

```bash theme={null}
prefix_a=one
prefix_b=two
echo ${!prefix_*}  # all variables names starting with `prefix_`
prefix_a prefix_b
```

### Indirection

```bash theme={null}
name=joe
pointer=name
echo ${!pointer}
joe
```

### Substitution

| `${foo%suffix}`   | Remove suffix       |
| :---------------- | :------------------ |
| `${foo#prefix}`   | Remove prefix       |
| `${foo%%suffix}`  | Remove long suffix  |
| `${foo/%suffix}`  | Remove long suffix  |
| `${foo##prefix}`  | Remove long prefix  |
| `${foo/#prefix}`  | Remove long prefix  |
| `${foo/from/to}`  | Replace first match |
| `${foo//from/to}` | Replace all         |
| `${foo/%from/to}` | Replace suffix      |
| `${foo/#from/to}` | Replace prefix      |

### Comments

```bash theme={null}
# Single line comment
```

```bash theme={null}
: '
This is a
multi line
comment
'
```

### Substrings

| `${foo:0:3}`    | Substring *(position, length)* |
| :-------------- | :----------------------------- |
| `${foo:(-3):3}` | Substring from the right       |

### Length

| `${#foo}` | Length of `$foo` |
| :-------- | :--------------- |

### Manipulation

```bash theme={null}
str="HELLO WORLD!"
echo "${str,}"   #=> "hELLO WORLD!" (lowercase 1st letter)
echo "${str,,}"  #=> "hello world!" (all lowercase)

str="hello world!"
echo "${str^}"   #=> "Hello world!" (uppercase 1st letter)
echo "${str^^}"  #=> "HELLO WORLD!" (all uppercase)
```

### Default values

| `${foo:-val}`     | `$foo`, or `val` if unset (or null)                      |
| :---------------- | :------------------------------------------------------- |
| `${foo:=val}`     | Set `$foo` to `val` if unset (or null)                   |
| `${foo:+val}`     | `val` if `$foo` is set (and not null)                    |
| `${foo:?message}` | Show error message and exit if `$foo` is unset (or null) |

<Note>
  Omitting the `:` removes the (non)nullity checks, e.g. `${foo-val}` expands to `val` if unset otherwise `$foo`.
</Note>

## Loops

***

### Basic for loop

```bash theme={null}
for i in /etc/rc.*; do
  echo "$i"
done
```

### C-like for loop

```bash theme={null}
for ((i = 0 ; i < 100 ; i++)); do
  echo "$i"
done
```

### Ranges

```bash theme={null}
for i in {1..5}; do
    echo "Welcome $i"
done
```

#### With step size

```bash theme={null}
for i in {5..50..5}; do
    echo "Welcome $i"
done
```

### Reading lines

```bash theme={null}
while read -r line; do
  echo "$line"
done <file.txt
```

### Forever

```bash theme={null}
while true; do
  ···
done
```

## Functions

***

### Defining functions

```bash theme={null}
myfunc() {
    echo "hello $1"
}
```

```bash theme={null}
# Same as above (alternate syntax)
function myfunc {
    echo "hello $1"
}
```

```bash theme={null}
myfunc "John"
```

### Returning values

```bash theme={null}
myfunc() {
    local myresult='some value'
    echo "$myresult"
}
```

```bash theme={null}
result=$(myfunc)
```

### Raising errors

```bash theme={null}
myfunc() {
  return 1
}
```

```bash theme={null}
if myfunc; then
  echo "success"
else
  echo "failure"
fi
```

### Arguments

| `$#` | Number of arguments                            |
| :--- | :--------------------------------------------- |
| `$*` | All positional arguments (as a single word)    |
| `$@` | All positional arguments (as separate strings) |
| `$1` | First argument                                 |
| `$_` | Last argument of the previous command          |

<Note>
  **Note**: `$@` and `$*` must be quoted in order to perform as described. Otherwise, they do exactly the same thing (arguments as separate strings).

  > See [Special parameters](https://web.archive.org/web/20230318164746/https://wiki.bash-hackers.org/syntax/shellvars#special_parameters_and_shell_variables).
</Note>

## Conditionals

### Conditions

<Note>
  Note that `[[` is actually a command/program that returns either `0` (true) or `1` (false). Any program that obeys the same logic (like all base utils, such as `grep(1)` or `ping(1)`) can be used as condition, see examples.
</Note>

| `[[ -z STRING ]]`        | Empty string          |
| :----------------------- | :-------------------- |
| `[[ -n STRING ]]`        | Not empty string      |
| `[[ STRING == STRING ]]` | Equal                 |
| `[[ STRING != STRING ]]` | Not Equal             |
| `[[ NUM -eq NUM ]]`      | Equal                 |
| `[[ NUM -ne NUM ]]`      | Not equal             |
| `[[ NUM -lt NUM ]]`      | Less than             |
| `[[ NUM -le NUM ]]`      | Less than or equal    |
| `[[ NUM -gt NUM ]]`      | Greater than          |
| `[[ NUM -ge NUM ]]`      | Greater than or equal |
| `[[ STRING =~ STRING ]]` | Regexp                |
| `(( NUM < NUM ))`        | Numeric conditions    |

#### More conditions

| `[[ -o noclobber ]]` | If OPTIONNAME is enabled |        |    |
| :------------------- | :----------------------- | ------ | -- |
| `[[ ! EXPR ]]`       | Not                      |        |    |
| `[[ X && Y ]]`       | And                      |        |    |
| \`\[\[ X             |                          | Y ]]\` | Or |

### File conditions

| `[[ -e FILE ]]`         | Exists                  |
| :---------------------- | :---------------------- |
| `[[ -r FILE ]]`         | Readable                |
| `[[ -h FILE ]]`         | Symlink                 |
| `[[ -d FILE ]]`         | Directory               |
| `[[ -w FILE ]]`         | Writable                |
| `[[ -s FILE ]]`         | Size is > 0 bytes       |
| `[[ -f FILE ]]`         | File                    |
| `[[ -x FILE ]]`         | Executable              |
| `[[ FILE1 -nt FILE2 ]]` | 1 is more recent than 2 |
| `[[ FILE1 -ot FILE2 ]]` | 2 is more recent than 1 |
| `[[ FILE1 -ef FILE2 ]]` | Same files              |

### Example

```bash theme={null}
# String
if [[ -z "$string" ]]; then
  echo "String is empty"
elif [[ -n "$string" ]]; then
  echo "String is not empty"
else
  echo "This never happens"
fi
```

```bash theme={null}
# Combinations
if [[ X && Y ]]; then
  ...
fi
```

```bash theme={null}
# Equal
if [[ "$A" == "$B" ]]
```

```bash theme={null}
# Regex
if [[ "A" =~ . ]]
```

```bash theme={null}
if (( $a < $b )); then
   echo "$a is smaller than $b"
fi
```

```bash theme={null}
if [[ -e "file.txt" ]]; then
  echo "file exists"
fi
```

## Arrays

***

### Defining arrays

```bash theme={null}
Fruits=('Apple' 'Banana' 'Orange')
```

```bash theme={null}
Fruits[0]="Apple"
Fruits[1]="Banana"
Fruits[2]="Orange"
```

### Working with arrays

```bash theme={null}
echo "${Fruits[0]}"           # Element #0
echo "${Fruits[-1]}"          # Last element
echo "${Fruits[@]}"           # All elements, space-separated
echo "${#Fruits[@]}"          # Number of elements
echo "${#Fruits}"             # String length of the 1st element
echo "${#Fruits[3]}"          # String length of the Nth element
echo "${Fruits[@]:3:2}"       # Range (from position 3, length 2)
echo "${!Fruits[@]}"          # Keys of all elements, space-separated
```

### Operations

```bash theme={null}
Fruits=("${Fruits[@]}" "Watermelon")    # Push
Fruits+=('Watermelon')                  # Also Push
Fruits=( "${Fruits[@]/Ap*/}" )          # Remove by regex match
unset Fruits[2]                         # Remove one item
Fruits=("${Fruits[@]}")                 # Duplicate
Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate
lines=(`cat "logfile"`)                 # Read from file
```

### Iteration

```bash theme={null}
for i in "${arrayName[@]}"; do
  echo "$i"
done
```

## Dictionaries

***

### Defining

```bash theme={null}
declare -A sounds
```

```bash theme={null}
sounds[dog]="bark"
sounds[cow]="moo"
sounds[bird]="tweet"
sounds[wolf]="howl"
```

Declares `sound` as a Dictionary object (aka associative array).

### Working with dictionaries

```bash theme={null}
echo "${sounds[dog]}" # Dog's sound
echo "${sounds[@]}"   # All values
echo "${!sounds[@]}"  # All keys
echo "${#sounds[@]}"  # Number of elements
unset sounds[dog]     # Delete dog
```

### Iteration

#### Iterate over values

```bash theme={null}
for val in "${sounds[@]}"; do
  echo "$val"
done
```

#### Iterate over keys

```bash theme={null}
for key in "${!sounds[@]}"; do
  echo "$key"
done
```

## Debugging/Options

### Options

```bash theme={null}
set -o noclobber  # Avoid overlay files (echo "hi" > foo)
set -o errexit    # Used to exit upon error, avoiding cascading errors
set -o pipefail   # Unveils hidden failures
set -o nounset    # Exposes unset variables
```

### Glob options

```bash theme={null}
shopt -s nullglob    # Non-matching globs are removed  ('*.foo' => '')
shopt -s failglob    # Non-matching globs throw errors
shopt -s nocaseglob  # Case insensitive globs
shopt -s dotglob     # Wildcards match dotfiles ("*.sh" => ".foo.sh")
shopt -s globstar    # Allow ** for recursive matches ('lib/**/*.rb' => 'lib/a/b/c.rb')
```

> Set `GLOBIGNORE` as a colon-separated list of patterns to be removed from glob matches.

## History

### Commands

| `history`             | Show history                              |
| :-------------------- | :---------------------------------------- |
| `shopt -s histverify` | Don’t execute expanded result immediately |

### Expansions

| `!$`         | Expand last parameter of most recent command         |
| :----------- | :--------------------------------------------------- |
| `!*`         | Expand all parameters of most recent command         |
| `!-n`        | Expand `n`th most recent command                     |
| `!n`         | Expand `n`th command in history                      |
| `!<command>` | Expand most recent invocation of command `<command>` |

### Operations

| `!!`                 | Execute last command again                                            |
| :------------------- | :-------------------------------------------------------------------- |
| `!!:s/<FROM>/<TO>/`  | Replace first occurrence of `<FROM>` to `<TO>` in most recent command |
| `!!:gs/<FROM>/<TO>/` | Replace all occurrences of `<FROM>` to `<TO>` in most recent command  |
| `!$:t`               | Expand only basename from last parameter of most recent command       |
| `!$:h`               | Expand only directory from last parameter of most recent command      |

<Info>
  `!!` and `!$` can be replaced with any valid expansion.
</Info>

### Slices

| `!!:n`   | Expand only `n`th token from most recent command (command is `0`; first argument is `1`) |
| :------- | :--------------------------------------------------------------------------------------- |
| `!^`     | Expand first argument from most recent command                                           |
| `!$`     | Expand last token from most recent command                                               |
| `!!:n-m` | Expand range of tokens from most recent command                                          |
| `!!:n-$` | Expand `n`th token to last from most recent command                                      |

<Info>
  `!!` can be replaced with any valid expansion i.e. `!cat`, `!-2`, `!42`, etc.
</Info>

## Miscellaneous

***

### Numeric calculations

```bash theme={null}
$((a + 200))      # Add 200 to $a
```

```bash theme={null}
$(($RANDOM%200))  # Random number 0..199
```

```bash theme={null}
declare -i count  # Declare as type integer
count+=1          # Increment
```

### Subshells

```bash theme={null}
(cd somedir; echo "I'm now in $PWD")
pwd # still in first directory
```

### Redirection

```bash theme={null}
python hello.py > output.txt            # stdout to (file)
python hello.py >> output.txt           # stdout to (file), append
python hello.py 2> error.log            # stderr to (file)
python hello.py 2>&1                    # stderr to stdout
python hello.py 2>/dev/null             # stderr to (null)
python hello.py >output.txt 2>&1        # stdout and stderr to (file), equivalent to &>
python hello.py &>/dev/null             # stdout and stderr to (null)
echo "$0: warning: too many users" >&2  # print diagnostic message to stderr
```

```bash theme={null}
python hello.py < foo.txt      # feed foo.txt to stdin for python
diff <(ls -r) <(ls)            # Compare two stdout without files
```

### Inspecting commands

```bash theme={null}
command -V cd
#=> "cd is a function/alias/whatever"
```

### Trap errors

```bash theme={null}
trap 'echo Error at about $LINENO' ERR
```

or

```bash theme={null}
traperr() {
  echo "ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO[0]}"
}

set -o errtrace
trap traperr ERR
```

### Case/switch

```bash theme={null}
case "$1" in
  start | up)
    vagrant up
    ;;

  *)
    echo "Usage: $0 {start|stop|ssh}"
    ;;
esac
```

### Source relative

```bash theme={null}
source "${0%/*}/../share/foo.sh"
```

### printf

```bash theme={null}
printf "Hello %s, I'm %s" Sven Olga
#=> "Hello Sven, I'm Olga

printf "1 + 1 = %d" 2
#=> "1 + 1 = 2"

printf "This is how you print a float: %f" 2
#=> "This is how you print a float: 2.000000"

printf '%s\n' '#!/bin/bash' 'echo hello' >file
# format string is applied to each group of arguments
printf '%i+%i=%i\n' 1 2 3  4 5 9
Transform strings
```

| `-c`        | Operations apply to characters not in the given set |
| :---------- | :-------------------------------------------------- |
| `-d`        | Delete characters                                   |
| `-s`        | Replaces repeated characters with single occurrence |
| `-t`        | Truncates                                           |
| `[:upper:]` | All upper case letters                              |
| `[:lower:]` | All lower case letters                              |
| `[:digit:]` | All digits                                          |
| `[:space:]` | All whitespace                                      |
| `[:alpha:]` | All letters                                         |
| `[:alnum:]` | All letters and digits                              |

#### Example

```bash theme={null}
echo "Welcome To heLL" | tr '[:lower:]' '[:upper:]'
WELCOME TO HELL
```

### Directory of script

```bash theme={null}
dir=${0%/*}
```

### Getting options

```bash theme={null}
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
  -V | --version )
    echo "$version"
    exit
    ;;
  -s | --string )
    shift; string=$1
    ;;
  -f | --flag )
    flag=1
    ;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi
```

### Heredoc

```bash theme={null}
cat <<END
hello world
END
```

### Reading input

```bash theme={null}
echo -n "Proceed? [y/n]: "
read -r ans
echo "$ans"
```

The `-r` option disables a peculiar legacy behavior with backslashes.

```bash theme={null}
read -n 1 ans    # Just one character
```

### Special variables

| `$?`               | Exit status of last task               |
| :----------------- | :------------------------------------- |
| `$!`               | PID of last background task            |
| `$$`               | PID of shell                           |
| `$0`               | Filename of the shell script           |
| `$_`               | Last argument of the previous command  |
| `${PIPESTATUS[n]}` | return value of piped commands (array) |

> See [Special parameters](https://web.archive.org/web/20230318164746/https://wiki.bash-hackers.org/syntax/shellvars#special_parameters_and_shell_variables).

### Go to previous directory

```bash theme={null}
pwd # /home/user/foo
cd bar/
pwd # /home/user/foo/bar
cd -
pwd # /home/user/foo
```

### Check for command’s result

```bash theme={null}
if ping -c 1 google.com; then
  echo "It appears you have a working internet connection"
fi
```

### Grep check

```bash theme={null}
if grep -q 'foo' ~/.bash_history; then
  echo "You appear to have typed 'foo' in the past"
fi
```
