• 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 not found

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.

If you encounter the below error while running the test command:

test: command not found

you may try installing the below package as per your choice of distribution:

Distribution Command
OS X brew install coreutils
Debian apt-get install coreutils
Ubuntu apt-get install coreutils
Alpine apk add coreutils
Arch Linux pacman -S coreutils
Kali Linux apt-get install coreutils
CentOS yum install coreutils
Fedora dnf install coreutils
Raspbian apt-get install coreutils

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 Setup SSH keys for “passwordless” SSH Login on CentOS/RHEL
  2. CentOS / RHEL : iptables troubleshooting guide
  3. last Command Examples in Linux
  4. numactl: command not found
  5. mycli Command Examples in Linux
  6. lspcmcia Command Examples in Linux
  7. ifrename: command not found
  8. How to check change log of RPM packages on CentOS/RHEL
  9. 5 Useful Command Examples to Monitor User Activity under Linux
  10. Interview Questions : Linux Package Manager (RPM)

You May Also Like

Primary Sidebar

Recent Posts

  • powertop Command Examples in Linux
  • powertop: command not found
  • powerstat: command not found
  • powerstat Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright