December 1999
Beginner
528 pages
11h 10m
English
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 ...
Read now
Unlock full access