Skip to content

Linux bash test statement


Testing options after if [[ ]] in bash programming:

1. Integer testing

-le    less equal
-lt    less than
-ge    greater equal
-gt    greater than
-eq    equal
-ne    not equal

2. Character testing

-n    test if string is not null
-s    test if string is null

3. File testing

-e    test if file exists
-f    test if file is a regular file
-d    test if path is a directory
-r    test if file is readable by current user
-w    test if file is writable by current user
-x    test if file is executable by current user

For example, check if a file exists:

if [[ -e /etc/passwd ]]; then
  echo "File exist"
  echo "File not exist"

Back to Table of Contents

  1. License under CC BY-NC 4.0
  2. Copyright issue feedback, replace # with @
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No personal information is collected.