Skip to content

Instantly share code, notes, and snippets.

@harishkannarao
Created November 21, 2018 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harishkannarao/4cdaa276457a5986b814cc5b1e8568e8 to your computer and use it in GitHub Desktop.
Save harishkannarao/4cdaa276457a5986b814cc5b1e8568e8 to your computer and use it in GitHub Desktop.

Bash Script loops and conditional statement example

The following blog post shows example of bash script with for loop, while loop, if elif else conditional statements.

Important aspects covered in the sample script:

  • Conditional while loop
  • While loop to iterate over lines in a file
  • Infinite while loop with break and continue
  • Infinite while loop with sleep
  • Iterate over arguments passed as input using for loop
  • For loop with counter
  • if..elif..else conditional structure
  • Numerical comparison
  • String comparison
  • Regular Expression match
  • and (&&), or (||), not (!) conditions
  • Expression with multiple conditions

Executing the script:

bash loops.sh --help

bash conditionals.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment