Command-line Basics: Shell Script Conditionals

joshtronic

Most computer programs, regardless of language, are just a bunch of conditional statements. These conditionals allow the control flow of a program to be altered instead of just running every single line of code all of the time. Outside of computer programming, conditionals are all around us, just a series of decisions being made and their outcomes.

Maybe we really are living inside a simulation. O_O

Getting started

To run the following shell scripting commands, you will need access to a system with a shell like bash or zsh available. This is standard issue on Unix-like systems like Linux and macOS and can be accessed on Windows and Chrome OS by way of their respective Linux subsystem offerings.

Even though we’re talking about scripts, the source code we will be writing can be entered directly into your shell prompt and executed. If you happen to goof while typing something in, don’t worry, you can simply hit CTRL-C to get back to a clean prompt.

Recommended Node.js video course

Basic conditional structure

Just like all of the conditionals you’ve encountered in other languages, conditionals in shell scripting consist of the same “if”, “else” and “else if” conditions, with slightly different syntax:

GATOR=true
SNAKE=false

# if
if [ $GATOR ]; then
  echo "We have a gator"
fi

if [ !$GATOR ]; then
  echo "We don't have a gator"
fi

# if / else
if [ $GATOR ]; then
  echo "We have a gator"
else
  echo "We don't have a gator"
fi

# if / else if / else
if [ $SNAKE ]; then
  echo "We have a snake"
elif [ $GATOR ]; then
  echo "We have a gator"
else
  echo "We don't either"
fi

Integer conditionals

When you’re doing integer conditionals, you use operators like -eq and -ge (equal and greater than or equal to respectively):

GATOR_SCORE = 100;
SNAKE_SCORE = 0;

if [ $GATOR_SCORE -gt $SNAKE_SCORE ]; then
  echo "The gator wins"
elif [ $GATOR_SCORE -lt $SNAKE_SCORE ]; then
  echo "The snake wins"
else
  echo "It's a tie"
fi

Integer operators

  • -eq is equal to
  • -ne is not equal to
  • -gt is greater than
  • -ge is great then or equal to
  • -lt is less than
  • -le is less than or equal to

String conditionals

Comparing strings is quite similar to comparing integers, but we have slightly different operators to use:

GATOR="alligator"
SNAKE="anaconda"

if [ $GATOR = $SNAKE ]; then
  echo "They are the same";
fi

if [ $GATOR != $SNAKE ]; then
  echo "They are different";
fi;

if [ $GATOR = $SNAKE ]; then
  echo "They are the same";
else
  echo "They are different";
fi;

You can also check strings for less than or greater than. Instead of being based on numeric value, it’s based on ASCII alphabetical order. When using these operators, you either need to escape them, or use “double brackets” [[ and ]] instead of [ and ]:

if [ $GATOR \< $SNAKE ]; then
  echo "alligator comes before snake";
fi;

if [[ $GATOR < $SNAKE ]]; then
  echo "alligator comes before snake";
fi;

String operators

  • = or == is equal to
  • != is not equal to
  • -z is null
  • -n is not null
  • < is less than in ASCII alphabetical order
  • > is greater than in ASCII alphabetical order

Conclusion

While not an exhaustive list of examples, these basic conditionals can still get you pretty far when writing scripts. Combine these conditionals with command-line arguments coming into your script or even user input and you can really take your script to the next level.

  Tweet It

🕵 Search Results

🔎 Searching...

Sponsored by #native_company# — Learn More
#native_title# #native_desc#
#native_cta#