How to Increment and Decrement Variable in Bash (Counter)

One of the most popular arithmetic operations when addressing Bash scripts is incrementing and decrementing variables. This is most regularly used in loops as a counter, but it can happen elsewhere in the script. Incrementing and Decrementing centers on adding or subtracting a value (usually 1), respectively, from the value of a numeric variable. The arithmetic augmentation can be performed using the double parentheses ((…)) and $((…)) or with the built-in let command. In bash, there are various ways to increment/decrement a variable. In this article, some are explained. Using + and – Operators The most easy way to increment/decrement a variable is by using the + and – operators. i=$((i+1)) ((i=i+1)) let “i=i+1” i=$((i-1)) ((i=i-1)) let “i=i-1” This method enables you to increment/decrement the variable by any value you want. Here is an example of incrementing a variable within an until loop: i=0 until [ $i -gt 3 ] do   echo i: $i   ((i=i+1)) done Output: i: 0 i: 1 i: 2 i: 3 The += and -= Operators In addition to the basic operators explained above, bash also provides the assignment operators += and -=. These operators are used to increment/decrement the left operand’s value with the value…

How to Check whether a Directory or File Exists in Bash

Numerous scenarios will arise where you may need to perform an action based on whether a file exists or not. While using the test command in Bash, you should determine whether a file exists and determine its file type. A test command can take one of three possible syntax: Test expression. [ EXPRESSION ] [[ EXPRESSION ]] If you want a script to be portable, you should use the available command on all POSIX shells. The latest version of the test command, [[ (double brackets), is supported on most modern systems using the Bash, Zsh, and Ksh as a default shell. Check if the file exists. When checking a specific file, the most commonly used FILE operations include -e and -f. The first one will verify any file’s existence regardless of what type of file it is, while the second one will only return true for those files that are regular files (not a directory or a device). The most efficient method of determining whether a file exists uses the test command and the if statement. Suppose you see any of the following, the /etc/resolv.conf file exists: FILE=/etc/resolv.conf if test -f “$FILE”; then echo “$FILE exists.” fi FILE=/etc/resolv.conf if […

Bash Concatenate Strings

Concatenation is one of the most popular and used string operations. String concatenation is just a decorative programming word for joining strings collectively by adding one string to another string’s end. In this article, we will show how to concatenate strings in bash. Concatenating Strings The easiest way to concatenate two or more string variables is to write them one after another: VAR1= “Hi,” VAR2=” Lucky” VAR3=”$VAR1 $VAR2″ echo “$VAR3.” The last line will echo the concatenated string: Output: Hi, Lucky With the help of a literal string, you can concatenate one or more variable: VAR1= “Hey,” VAR2= “${VAR1}World.” echo “$VAR2.” Output: Hello, World The example over variable VAR1 is enveloped in curly braces to guard the variable name against surrounding characters. When another valid variable-name character reflects the variable, you must have it in curly braces ${VAR1}. To circumvent any word splitting or globbing issues, you should regularly try to use double quotes nearby the variable name if you want to suppress variable addition and special treatment of the backslash character rather than dual-use single quotes. Bash does not separate variables by “type”; variables are used as integer or string depending on contexts. You can also combine variables that…