Updated: 06 July 2023
bash
bash array
Updated: 23 June 2023
Bash does not support multi-dimensional arrays.
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)
bash sort
Updated: 01 January 2023
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: 31 December 2022
until loop
Very similar to the 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
bash exit code
Updated: 12 February 2023
#!/bin/bash -xv
# run date command
date
# exit code is in $?
status=$?
[ $status -eq 0 ] && echo "success" || echo "failure"
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."