Conditionals and Tests
Part of the Bash course
Every script you have written so far runs the same lines no matter what. Real scripts need to make decisions: "only do this if that file exists", "only continue if the last command worked". This lesson teaches you how Bash asks those questions and acts on the answer. Nothing here assumes you have written a decision-making program before.
In lesson 02 you met the exit code: a hidden number every command produces when it finishes, where 0 means success and anything else means failure. Conditionals in Bash are built entirely on that idea. Where other languages ask "is this true or false?", Bash asks "did this command succeed or fail?" and treats success as yes: if does not look at a value, it runs a command and reacts to whether its exit code was 0. ...
Want to learn Bash Conditionals and Tests in a real environment?
Click below, get a fresh Linux box with Bash ready to go, and work through Conditionals and Tests hands-on with an AI tutor that knows your code.
Start this lessonConditionals and TestsWhat you'll learn
- If statement
- [[ ]] tests
- String tests
- File tests -f -d -e
- && and ||
- Case statement
Read this lesson in your language
All lessons in this course
12 lessons- 1What Is Bash20 min
- 2Shebang and Your First Script25 min
- 3Variables and Quoting25 min
- 4Conditionals and Tests30 min
- 5Loops25 min
- 6Functions25 min
- 7Arrays25 min
- 8String and Arithmetic Expansion25 min
- 9IO Redirection and Pipes25 min
- 10Error Handling and Traps30 min
- 11Debugging and Best Practices25 min
- 12Capstone: Server Bootstrap Script60 min
