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

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

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