Skip to Content
Learning Linux Shell Scripting - Second Edition
book

Learning Linux Shell Scripting - Second Edition

by Ganesh Sanjiv Naik
May 2018
Beginner
332 pages
7h 28m
English
Packt Publishing
Content preview from Learning Linux Shell Scripting - Second Edition

Using the test command

Let's look at the following example to check the content or value of expressions:

    $ test $name=Ganesh
    $ echo $?
    0 if success and 1 if failure.

In the preceding example, we want to check whether the content of the variable name is the same as Ganesh and ?. To check this, we have used the test command. The test command will store the result of the comparison in the ? variable.

We can use the following syntax for the preceding test command. In this case, we used [ ] instead of the test command. We've enclosed the expression to be evaluated in square brackets:

    $ [[ $name = Ganesh ]]      # Brackets replace the test command
    $ echo $?
    0

During the evaluation of expressions by test, we can even use wildcard expressions:

    $ [[ ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Linux Shell Scripting

Learning Linux Shell Scripting

Ganesh Sanjiv Naik
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar

Publisher Resources

ISBN: 9781788993197Supplemental Content