Mastering the test command in Linux: A Comprehensive Guide with Examples


test command in Linux means Checking file types and comparing values. In the Linux operating system, checking conditions and taking actions based on those checks is a crucial skill for users, especially when writing shell scripts. One of the most important commands for performing these condition checks is the test command.

This command allows you to check file attributes, strings, and numbers, and based on the results, you can determine the next steps in your script. In this article, we will explore the test command in detail, how to use it, and provide practical examples that you can apply in your daily tasks.

test command in Linux with Examples

test command syntax in Linux

[ ]

According to the man page, the test command checks file types and compares values.

For more detailed information about the test command, you can use:

man test

test command in Linux with Examples

To compare two numbers, use:

if [ $num1 -lt $num2 ]; then
  echo "$num1 is less than $num2"
  echo "$num1 is not less than $num2"

Or using a direct test in a single line:

$ test 10 -gt 5 && echo "Yes"

Checking Multiple Conditions with Logical AND

if [ -e /path/to/file ] && [ -w /path/to/file ]; then
  echo "File exists and is writable."
  echo "File does not exist or is not writable."

To check if a file is not empty, use:

if [ -s /path/to/file ]; then
  echo "File is not empty."
  echo "File is empty."

To combine logical operators, use:

if [ -e /path/to/file ] && ([ "$str1" = "$str2" ] || [ $num1 -lt $num2 ]); then
  echo "Conditions met."
  echo "Conditions not met."


The test command is a powerful and flexible tool in Linux for performing condition checks. By mastering its options and syntax, you can write more efficient shell scripts and manage automation tasks with ease. Hopefully, this article has given you a clearer understanding of how to use the test command examples and how to apply it to your daily tasks. Keep exploring and leveraging the powerful tools of Linux to enhance your work efficiency and system management. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.