test Command in Linux

Learn via video courses
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

In Linux, the test command is a built-in function of the bash shell. It enables us to check file types and compare values. Commonly used in scripting languages, the test command in Linux also facilitates decision making in shell scripts by evaluating conditional expressions.

Syntax of test Command in Linux

The basic syntax of the test command in Linux is as follows:

Where:

  • test: This is the command itself.
  • EXPRESSION: This is the condition that you want to test.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Options in test Command in Linux

  1. -s FILE: This option checks if FILE exists and has a size greater than zero.

    For example -

    Output:

    This command checks if the file 'myfile.txt' exists and is not empty.

  2. -f FILE: This option checks if FILE exists and is a regular file.

    For example -

    Output:

    This command checks if 'myfile.txt' exists and is a regular file.

  3. -eq: This option checks if two numbers are equal.

    For example -

    Output:

    This command checks if 5 equals 5.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Example Usages

  • To test if a file exists and is not empty:

    Output:

    Explanation: The command checks if 'myfile.txt' exists and is not empty.

  • To check if two numbers are equal:

    Output:

    Explanation: The command checks if 5 equals 5.

Tips

  • test command returns a status of 0 (true) and 1 (false) to enable further script control.

  • test command is often used in combination with if-else statements in bash scripts.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Advanced Use Cases of test Command in Linux

  • Combining multiple conditions:

    Output:

    Explanation: The command checks if 'myfile.txt' exists, is not empty, and is a regular file.

  • Using 'test' command within an if-else construct:

    Output:

    Explanation: The command checks if 'myfile.txt' exists and is a regular file, and prints a corresponding message.

  • Checking for inequality using 'test' command:

    Output:

    Explanation: The command checks if 5 is not equal to 10.

Conclusion

  • The test command in Linux is a versatile tool for evaluating expressions, commonly used within scripts.

  • It returns a status of 0 for true and 1 for false, useful for script control flow.

  • The test command can check for file properties and compare numerical or string values.

  • It can be used in complex expressions by combining multiple conditions.