Using file attributes with conditional logic

Earlier in this book, we touched upon various tests for strings, numbers, and variables. Using a similar concept built into Bash, we can also use various attributes to test against files and directories. This extends upon the introduction conditional logic to perform tests on files. Does an example exist? Is it a directory? and so on.

For a moment, though, couldn't we just use the results from executing and checking the return code? Absolutely! This is another method you can use, especially if you are using a version of Bash that supports all of Bashes features. It is just another way to "skin the rabbit".

Let's start off first with some of the common flags, which return true if:

  • -e: The file ...

Get Bash Cookbook 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.