• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

test Command Examples in Linux

by admin

The test command is used to check conditional logic and perform comparisons. You can use the test command in your shell scripts to validate the status of files and perform relevant tasks. It evaluates a conditional expression or logical operation and displays an exit status. The exit status is 0 if the expression is true and 1 if the expression is false.

For example:

var=/etc

if test -d $var;
then
    echo "The $var directory exists!"
fi

This example uses the -d option to test if a directory exists. There are many such conditional options you can use. Consult the man page for the test command to see them all.

test Command Examples

1. Test if a given variable is equal to a given string:

# test "$MY_VAR" == "/bin/zsh"

2. Test if a given variable is empty:

# test -z "$GIT_BRANCH"

3. Test if a file exists:

# test -f "path/to/file_or_directory"

4. Test if a directory does not exist:

# test ! -d "path/to/directory"

5. If A is true, then do B, or C in the case of an error (notice that C may run even if A fails):

# test condition && echo "true" || echo "false"

Filed Under: Linux

Some more articles you might also be interested in …

  1. blkdiscard: command not found
  2. How To Execute The Pstack Command On CentOS/RHEL
  3. git info: Display Git repository information
  4. Chezmoi: A multi-machine dotfile manager, written in Go
  5. lftp Command Examples in Linux
  6. Spacewalk Installation Steps On CentOS/RHEL
  7. “git stamp” Command Examples
  8. “git effort” Command Examples
  9. Beginners Guide to Linux Software Management with RPM
  10. netplan: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • glab Command Examples
  • “glab repo” Command Examples
  • “glab release” Command Examples
  • “glab pipeline” Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright