27 February 2022

Bash offers many built in conditional expressions that could be used to create bash program.

Note: all example below could be execute in a container:

docker run -it  ubuntu /bin/bash

Some Example

File exists and is a regular file

touch /tmp/file-exists.txt && test -f /tmp/file-exists.txt
echo $?
0
test -f not-existing-file.txt
echo $?
1

Arithmetic binary operators (equals)

test 1 -eq 1
echo $?
0
test 1 -eq 2
echo $?
1

References