bash background jobs

Updated: 29 August 2026

  1. Ctrl + Z push a running process to the background.
  2. Ctrl + Z push another running process to the background.
  3. jobs -l view background and suspended jobs attached to current terminal session.
  4. bg %1 resume job 1 in the background. Can omit number if only 1 job.
  5. fg %1 bring job 1 back to the foreground.
  6. kill %2 kill job number 2.

shfmt

Updated: 28 February 2025

A shell program formatter https://github.com/mvdan/sh

Check a shell program and specify 4 spaces of indentation

shfmt -i 4 my-bash-script.sh

Show a diff between actual and desired formatting

shfmt -d my-bash-script.sh

Directly update the file being checked

shfmt -w my-bash-script.sh

Run shfmt from Doccker

docker run --rm -it mvdan/shfmt:latest -ci -bn --diff -i=4 < check-me.sh

bash special parameters

Updated: 06 February 2025

File my_script.sh

#!/bin/bash

b_func(){
    echo ------
    for a in "$@"; do
      echo "$a"
    done
    echo ------
}

a_func(){
    # name of script or shell
    echo "$0"

    # 1st arg
    echo "$1"

    # 2nd arg
    echo "$2"

    # num of args
    echo $#

    # all args
    echo "$@"

    # all args
    echo "$*"

    # probably what you want
    b_func "$@"

    b_func $@

    b_func "$*"

    b_func $*
}

a_func 'a b' c

false

# last command exit status
echo $?

Result of running ./my_script

./my_script.sh
a b
c
2
a b c
a b c
------
a b
c
------
------
a
b
c
------
------
a b c
------
------
a
b
c
------
1

See https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html

bash IFS

Updated: 07 March 2024

Explode a string about a comma character, using the Internal Field Separator variable

mystr=foo,bar,spam

IFS=',' read -ra frags <<< $mystr
for frag in "${frags[@]}"; do
  echo "$frag"
done

# output
foo
bar
spam

bash functions

Updated: 25 January 2025

Taken from Advanced Bash-Scripting Guide: functions

#!/bin/bash

JUST_A_SECOND=1

funky () { echo "Call to funky"; echo "Exiting funky"; }

fun ()
{
  i=0
  REPEATS=4

  echo
  echo "And now the fun really begins."
  echo

  sleep $JUST_A_SECOND    # Hey, wait a second!
  while [ $i -lt $REPEATS ]
  do
    echo "<----------FUNCTIONS---------->"
    echo "<------------ARE-------------->"
    echo "<------------FUN-------------->"
    echo
    let "i+=1"
  done
}

funky
fun

# Exit with status of the last executed command.
exit $?

bash array

Updated: 26 September 2025

  • Bash does not support multi-dimensional arrays.

Print all elements of an array, each on a new line

printf '%s\n' "${our_array[@]}"

zsh

Updated: 17 June 2023

Files added by new install

.zshenv
.zprofile
.zshrc
.zlogin
.histfile

bash history

Updated: 15 April 2023

History doesn’t get written to .bash_history until log off.

Show history

history

Clear session history

history -c

Delete item 234 from history

history -d 234

Writes all current session command history to the HISTFILE

history -w

Search history

Ctrl+R and start typing the previous command. Once a result appears, repeat Ctrl+R to see other matches. Enter to run command.

To re-run a command from history

!<item number>