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 examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe