I'm looking at the '.profile' file in my home directory and I see different options used with the IF statement:
if [ -n "$BASH_VERSION" ]; then...
...
if [ -f "$HOME/.bashrc" ]; then...
...
if [ -d "$HOME/bin" ] ; then...
...What is the meaning and use of the -n -f and -d options here and generally in other bash scripts?
2 Answers
All BASH options for if-clauses can be found under the section CONDITIONAL EXPRESSIONS of the documentation (man bash).
Quoting it:
-d file
True if file exists and is a directory.
-f file
True if file exists and is a regular file.
-n string
True if the length of string is non-zero.
-f and -d are file test operators options, namely:
-f file is a regular file, not a directory
-d file is a directory
-n is comparison operator option. It returns "true" when the string following the option is not null.