List of prime numbers till 100 shell script program

broken image
broken image
broken image

To do that we will have to apply the following changes: We now want to pass N via the command line. Passing N as an Argument Through the Command Line The Bash script does print the odd numbers from 1 to 20. Note: Arithmetic operations can be executed in a Bash script using $(( )). Increment the value of INDEX by 1 before executing the next iteration of the while loop.Check if the REMAINDER is not zero…in that case print INDEX because it’s an odd number.At every iteration of the loop calculate the REMAINDER for the division of the INDEX by 2.Start a while loop that is executed if the value of INDEX is less than 20.Set the value of the variable INDEX to 0.How does this apply to this specific case? Print Odd Numbers Using a Bash Script INDEX=0Īnd if we translate this logic into an actual Bash script… #!/bin/bash This applies to any Bash shell running on Unix-like systems (including MacOS).įirst of all let’s have a look at the generic structure of a while loop in Bash: while Īs long as the condition between square brackets is true the commands between do and done are executed. You will see how our script looks like if we hardcode the value of N in it, and then you will learn how to pass the value of N to the script as an argument via the Linux command line. Let’s create a loop that goes through N numbers and prints only the odd ones.

broken image