There are quite a few ways to increment and decrement numeric variables in bash. This post examines the many ways you can do this. Credit: fancycrave1 When preparing scripts that will run in bash, it’s often critical to be able to set up numeric variables that you can then increment or decrement as the script proceeds. The only surprising part of this is how many options you have to choose from to make the increment or decrement operation happen. Incrementing numeric variables First, to increment a variable, you first need to set it up. While the example below sets the variable $count to 1, there is no need to start at 1. $ count=1 This would also work: $ count=111 Regardless of the initial setting, you can then increment your variable using any of the following commands. Just 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 should be one larger than it was before the command was run. As you likely suspect, except for the ++ command options, you can increase the value of a numeric value by more than one whenever you want. Try all of the commands shown below and you will see the end result. The final result should be 9 as the variable is incremented in each of the eight 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 below, the result 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 same way. You just need to use minus signs in place of the plus signs shown above. Here are the commands for decrementing the value 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 shown above have been run. Except for the — command options, you can decrease the value of a numeric value by more than one when needed. Here’s an example: $ count=11 $ ((count-=5)) $ echo $count 6 Incrementing variables using Loops If you want 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 once $count reaches 6 and each pass through the loop will display lines in a file named “myfile”, one more 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 shown about with nearly 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 numeric variables in bash is easy, but there are a lot more options than you likely expected. Related content how-to How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe