Updated: 10 January 2023
shell
Argbash
Updated: 30 January 2025
Create an argbash template file
argbash-init --pos positional-arg --opt optional-arg minimal.m4
Use the template file to create a script
argbash minimal.m4 -o my-script.sh
Re-generate script after editing Argbash section
argbash my-script.sh -o my-script.sh
bash sort
Updated: 04 March 2024
Sort by second column of tabulated data
$ printf "101\tc\n102\tb\n103\ta\n"
101 c
102 b
103 a
$ printf "101\tc\n102\tb\n103\ta\n" | sort -k2
103 a
102 b
101 c
bash expand
Updated: 01 January 2023
shell-expand-line
Check if shell-expand-line is bound to any keys
$ bind -P | grep shell-expand-line
If not add this to .inputrc
"\e\C-e": shell-expand-line
Test it
$ $HOME # Ctr+Alt+e, should print full path.
bash quoting
Updated: 31 December 2022
Single quotes
Single quoting is strong quoting, it preserves the literal value of each character
chris@ctd:~$ echo 'My home dir is $HOME' My home dir is $HOME
bash loops
Updated: 14 December 2025
until loop
Similar to a while loop, except that the loop executes until the test-command executes successfully. As long as this command fails, the loop continues.
until test-command; do
consequent-commands;
done
for loop
my_array=("apple" "banana" "orange" "grape")
for element in "${my_array[@]}"; do
echo "Current element: $element"
done
Alternatively
for run in {11..15}; do
echo 'it works?'
done
bash exit code
Updated: 07 December 2024
Simple exit status example
#!/bin/bash
date # run date command
status=$? # exit code is in $?
[ $status -eq 0 ] && echo "success" || echo "failure"
set -e Exit immediately if a pipeline, which may consist of a single simple command, a list, or a compound command returns a non-zero status
#!/bin/bash -e
rm does-not-exist.cs
ls # this command never runs
bash debugging
Updated: 03 May 2023
see https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html
Start subshell with -x option, which will run the entire script in debug mode. Traces of each command plus its arguments are printed to standard output after the commands have been expanded but before they are executed.
bash -x script1.sh
Debugging can be activated for a portion of the script
set -x # activate debugging from here
call_my_func
set +x # stop debugging from here
Other useful bash options
| Short notation | Long notation | Result |
|---|---|---|
| set -v | set -o verbose | Prints shell input lines as they are read. |
| set -x | set -o xtrace | Print command traces before executing command. |
Alternatively, these modes can be specified in the script itself, by adding the desired options to the first line shell declaration. Options can be combined, as is usually the case with UNIX commands
#!/bin/bash -xv
Examples of using echo for debugging
echo "debug message: now attempting to start w command"; w
echo "Variable VARNAME is now set to $VARNAME."
bash export
Updated: 28 December 2022
See https://www.digitalocean.com/community/tutorials/export-command-linux
Export is a built-in command of the Bash shell. It is used to mark variables and functions to be passed to child processes.
Display all exported variables
export
View all exported variables on the current shell
export -p
Declare and export a variable
export STUDENT=Divya
Display a variable
printenv STUDENT
Remove an environment variable
unset STUDENT
bash
Updated: 02 January 2026
Documentation
Bash guide for beginners
Bash reference manual
Bash reference manual
Bash conditional expressions
Miscellaneous
Shorten prompt in current terminal
PS1='\u:\W\$ '
Exit status of last command
echo $?
Repeat a command n times
for run in {1..100}; do command; sleep 5; done
Run A and then B, regardless of success of A
A; B
Run B if and only if A succeeded
A && B
Run B if and only if A failed
A || B
Run A in background.
A &
Escape ampersand (with backslash) in url
curl www.jerseyfinance.je/directory?key=foo\&cat=bar
Output a literal new line
printf 'foo\nbar\n'