Incrementing and decrementing numeric variables in bash

Uncategorized

When preparing scripts that will run in bash, it’s typically critical to be able to set up numerical variables that you can then increment or decrement as the script proceeds. The only unexpected part of this is how many choices you need to choose from to make the increment or decrement operation happen.Incrementing numeric

variables Initially, to increment a variable

, you initially need to set it up. While the example below sets the variable $count to 1, there is no need to begin at 1.$ count=1 This would also work:$count=111 Regardless

of the preliminary

setting, you can then increment your variable utilizing any of the following commands. Simply replace $count with your variable name.count= $((count +1) )((count=count +1)) let”count=count +1″ let “count++” let “++count” ((count+=1)) ((count++)) ((++count))

After running any of the above commands, $count must be one larger than it was before the command was run.As you likely suspect, except for the ++ command choices, you can increase the value of a numeric value by more than one whenever you desire. Attempt all of the commands shown below and you will see the end result. The outcome must be 9 as the variable is incremented in each of the 8 commands following the “count=1” command.

count=1 count=$((count +1)) ((count=count +1)) let “count=count +1” let “count++” let “++count” ((count+=1)) ((count++)) ((++count)) echo $count

In the example script listed below, the outcome would be 15 since three of the commands add more than 1 to $count.

#!/ bin/bash count=1 count=$((count +1)) ((count=count +2)) # add 2 let “count=count +3” # add 3 let “count++” let “++count” ((count+=4)) # add 4 ((count++)) ((++count)) echo $count

Decrementing numeric variables

Decrementing numbers can be done in the very same way. You simply require to use minus check in location of the plus indications revealed above. Here are the commands for decrementing the worth of $count:

count=$((count-1)) ((count=count-1)) let “count=count-1” let “count–” let “– count” ((count-=1)) ((count–)) ((– count))

If $count starts with a value of 20, it ends up being 12 when all of the commands revealed above have actually been run.Except for the–

command alternatives, you can decrease the worth of a numeric worth by more than one when required. Here’s an example:$count=11$( (count-=5)) $ echo $count 6

Incrementing variables using Loops

If you wish to loop through a series of commands in a script, you can increment a numeric variable to determine when the loop will end. The for loop shown below will end as soon as $count reaches 6 and each travel through the loop will display lines in a file called “myfile”, another line each time through the loop.

#!/ bin/bash count=1 while [$ count -le 5] do head -$count myfile ((count++)) echo done

Decrementing variables in loops

You can do the reverse of what is revealed about with almost the same code. This time, the $count variable starts at 5 and the loop exits when $count reaches 0.

#!/ bin/bash count=5 while [$ count -ge 1] do head -$count myfile ((count–)) done

Wrap-up

Incrementing and decrementing numerical variables in celebration is easy, but there are a lot more alternatives than you likely expected.

Copyright © 2023 IDG Communications, Inc.

. Source

Leave a Reply

Your email address will not be published. Required fields are marked *