Updated: 11 March 2026
https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html
Freelance software engineer United Kingdom
Updated: 11 March 2026
https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html
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
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
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 $?
Updated: 26 September 2025
Print all elements of an array, each on a new line
printf '%s\n' "${our_array[@]}"
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
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>
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)
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
Updated: 01 January 2023
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.
Updated: 31 December 2022
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