Chapter 17. Conditional testing

When scripting, you will probably want to be able to test whether a string is equal to another, or maybe check on a file status or even a numeric test. Based on the results of the test you can take further action. The test command can be used to test for strings, file status and numbers. It also fits in very well with the conditional if, then, else construct, as you’ll see in the next chapter.

In this chapter we will cover:

  • using the test command on files, strings and numbers; and

  • using the expr command on numbers and strings.

The expr tests and performs numeric output. Both test and expr will return 0 for true and 1 for false using the last exit status command $?.

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.