bash

Updated: 17 May 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'

Empty a file

> claude.log

Empty a file

cat /dev/null > claude.log

stderr and stdout

Updated: 04 May 2023

0 – stdin the standard input stream.
1 – stdout the standard output stream.
2 – stderr the standard error stream.

Redirecting output
ls -la > file.out

is equivalent to
ls -la 1> file.out

To redirect stderr
command 2> file.err

Sending both stderr and stdout to separate files
command 2> error.txt 1> output.txt

To suppress error messages in the terminal redirect to /dev/null
command 2> /dev/null

Redirect stderr to the current location of stdout and have error messages sent to the same file as standard output
command > file 2>&1