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

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • 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. How to use Magic SysRq tool in CentOS / RHEL
  2. Creating and Removing Files and Directories Under Linux
  3. chrt : command not found
  4. How to Provide Credentials From a File While Mounting CIFS Share in Linux
  5. CentOS / RHEL 6 : How to add/remove additional IP addresses to a network interface
  6. lftp Command Examples in Linux
  7. iostat: command not found
  8. links Command Examples in Linux
  9. How to find if NUMA configuration is enabled or disabled?
  10. opkg Command Examples in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • protonvpn-cli Command Examples in Linux
  • protonvpn-cli connect Command Examples
  • procs Command Examples in Linux
  • prlimit: command not found

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright