Here's a preview from my zine, Bite Size Bash! If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!
get the zine!
read the transcript!
the basic syntax
if COMMAND then
# do thing
else
# do other thing
fi
(you need a new line or ; before then)
[ vs [[
there are 2 commands often used in if statements: [ and [[
if [ -e file.txt ]
/usr/bin/[ (aka test) is a program that returns 0 if the test you pass it succeeds
if [[ -e file.txt ]]
[[ is built into bash. It lets you do tests like [[e x.txt && -e y.txt ]] that wouldn’t work with a command line tool
if COMMAND
did COMMAND return 0?
if ! COMMAND
did COMMAND NOT return 0?
if true
true always returns 0 :)
if [ -n "$var" ]
is $var nonempty?
if [ e file.txt ]
does file.txt exist?
combine with && and ||
if [ -e file] && [ -e file2 ]
if [ -d somedir ]
does somedir exist?
if [ -x script.sh ]
is script.sh executable?
man [ for more
you can do a lot!