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

Logical test operators

The following are the various options for logical operations using test, which is taken from the Bash reference manual available at http://www.gnu.org/software/bash/:

Test Operator
Tests True If
[ string_1 -a string_1 ]
Both string_1 and string_2 are true
[ string_1 -o string_2 ]
Either string_1 or string_2 is true
[ ! string_1 ]
Not a string_1 match
[[ pattern_1 && pattern_2 ]]
Both pattern_1 and pattern_2 are true
[[ pattern_1 || pattern_2 ]]
Either pattern_1 or pattern_2 is true
[[ ! pattern ]]
Not a pattern match

 

We can use the test operator for strings along with pattern matching as follows:

$ name=Ganesh $ [[ $name == [Gg]anesh ]] # Wildcards allowed $ echo $? 0 ...
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