shfmt

Updated: 22 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 -i mvdan/shfmt:v3.10.0 -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: 05 February 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>

bash process substitution

Updated: 11 January 2023

>(command_list)
<(command_list)

Process substitution feeds the output of a process (or processes) into the stdin of another process. It uses /dev/fd/<n> files to send the results of the process(es) within parentheses to another process. Effectively, process substitution turns a command into a temporary file, which is removed when the command completes.

cat <(echo hello world) # hello world
echo <(echo hello world) # /dev/fd/63

The first command converts the output of echo hello world into a file with contents hello world. The second command shows us the temp file in use.

The diff command requires files as arguments. So, comparing the contents of two directories can be achieved with diff and process substitution

diff <(ls /bin) <(ls /usr/bin)