User Tools

Site Tools


programming:bash:conditionals

This is an old revision of the document!


Table of Contents

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 for use with file paths.

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

programming/bash/conditionals.1517205444.txt.gz · Last modified: 2018-07-03 01:00 (external edit)