Concatenating Strings And Using += In Bash | Network World| ItSoftNews

It’s quite easy to get bash to concatenate strings and do simple math on Linux, but there are a number of options for you to use. This post focusses on concatenating strings, but also shows how one of the operators (+=) also plays a primary role in incrementing numbers.

Concatenating strings

In general, the only time that you’d want to concatenate strings on Linux is when one string is already defined and you want to add more to it. For example, if you have a script that greets the person running it, you might set up a string in the script to prepare the greeting and then add the person’s username or name before displaying it.

In this first example, we create a welcome string in a script and then pull the user’s first name from the /etc/passwd file and include it in the displayed text.

#!/bin/bash greeting="Welcome! Let's get started with your annual interview" name=`grep ^$USER /etc/passwd | awk -F: '{print $5}' | awk '{print $1}'` greet="$greeting, $name"  echo $greet 

When the script is run, it will start like this:

$ ./greet Welcome! Let's get started with your annual interview, Sandra 

The greeting was composed by simply including both the predefined greeting and the user’s first name in a variable named “greet”. Whether you create one string from two or simply use both in an echo command makes little difference. We could have easily displayed the greeting like this instead of creating separate $greeting and $greet variables:

echo "Welcome! Let's get started with your annual interview, $name" 

You can also add to an existing greeting without creating an additional variable as in these three command sequences. Notice how, in the first one, the $greet variable is included in the command that redefines it.

$ greet="Welcome! Let's get started with your annual interview" $ greet="${greet}, $USER" $ echo $greet Welcome! Let's get started with your annual interview, shs 

The following command, using printf, has the same effect.

$ greet="Welcome! Let's get started with your annual interview, " $ greet=$(printf "%s $USER" "$greet") $ echo $greet Welcome! Let's get started with your annual interview, shs 

This last approach combines three variables to create the greeting and uses the full username from the /etc/passwd file.

$ greet="Welcome! Let's get started with your annual interview" $ punct=", " $ target=`grep ^$USER /etc/passwd | awk -F: '{print $5}'` $ echo "$greet$punct$target" Welcome! Let's get started with your annual interview, Sandra Henry-Stocker 

The += sequence shown in the next example appends a name to the event variable:

$ event="Happy Birthday" $ event+=", Amanda" $ echo $event Happy Birthday, Amanda 

Incrementing numbers

This += sequence can be used to increment numbers. In the examples below, we set up a variable, add 1 to its value and then add 14. Notice how the ++ (add 1) and += (add specified number) sequences differ.

$ x=10 $ ((++x)) $ echo $x 11 $ ((x+=14)) $ echo $x 25 

NOTE: Using += without the double parentheses does something altogether different. It treats the digits as strings and concatenates them.

$ y=12; echo $y 12 $ y+=34; echo $y 1234 

To use the += sequence to add to an array, you can do something like what you see below. First, we define $x as shown below, creating the array in the process.

$ x=25
$ echo ${x[@]} 25

Then we can add a second element using the += sequence like this:

$ x+=(34) 

When we display the array again, we see it now has two elements:

$ echo ${x[@]} 25 34 

You can display the size of an array with a command like this:

$ echo ${#x[@]} 2 

Wrap-Up

Manipulating strings by defining separate portions and then using them together can make using the strings in scripts more convenient when only small portion of the text is likely to vary. The += sequence provides useful options both in manipulating both strings and numbers, but the latter can involve math as well as string concatenation. It’s good to know which one to expect.

Leave a Reply

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