User Tools

Site Tools


programming:bash:conditionals

Conditionals

Conditionals are the primary means of making decisions in programming, especially Bash scripting. Conditionals in Bash are with the [ and [[ built-ins, which are also commands! It makes reasoning about conditionals challenging at first, but there are some useful flags available:

Files

-a file
True if file exists.
-b file
True if file is an existing block device.
-c file
True if file is an existing character device
-d file
True if file is an existing directory.
-e
See -a.
-f file
True if file is an existing normal file.
-h file
True if file is a symbolic link (symlink).
-r file
True if file exists and is readable.
-s file
True if file exists and has a size greater than 0.
-w file
True if file exists and is writable.
-x file
True if file exists and is executable.

Variables

-v arg
True if arg is a set variable. :!: Bash 4.2+ only.

References

  • man bash — See "CONDITIONAL EXPRESSIONS"
programming/bash/conditionals.txt · Last modified: 2018-07-03 01:00 by 127.0.0.1