Skip to content

Latest commit

 

History

History
30 lines (21 loc) · 1.11 KB

if-statements-in-bash-scripts.md

File metadata and controls

30 lines (21 loc) · 1.11 KB

If Statements In Bash Scripts

Ok, so this is nothing mind-blowing. I may spend a lot of time in bash, but very little of that goes into writing scripts. Well, today I was downloading websites using a wget script I wrote specifically for that purpose, and had to add an if statement to construct some conditional wget arguments.

Here's how I thought a bash / shell if statement would work. (I confess, I did have to Google this one, and found a great article with examples.)

if [ $WGET_OPTS -eq 1 ] then
  // blabla
fi

This threw an error!

Here is the correct syntax:

if [ $WGET_OPTS -eq 1 ]
then
  // blabla
fi

Today I Learned: If statements in shell scripts require the then keyword to be on a separate line.

(As a sidenote, this should be considered under the age-old question: "Should curly braces appear on their own line?")