17.3. Testing strings

Testing strings is an important part of error trapping, especially when you need to test users input, or compare any variables. To test strings you have the choice of five formats.

test "string" test string_operator "string" test "string" string_operator "string" [ string_operator string ] [ string string_operator string ] 

where the string_operator can be:

= The two strings are equal
!= The two strings are not equal
-z This string is null
-n This string is not null

To test if the environment variable EDITOR is empty:

						$ [ -z $EDITOR ] 
$ echo $? 
1
					

No, it isn’t. Is it set to vi ?

						$ [ $EDITOR = "vi" ] 
$ echo $? 
0
					

Yes it is. Let’s verify that with an echo.

						$ echo $EDITOR 
vi
					

To test if the variable tape is equal to the ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.