O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required