Glam Prestige Journal

Bright entertainment trends with youth appeal.

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?

3

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy