There are many ways to concatenate strings when you're using bash and += might just surprise you! Credit: Maxiphoto / Getty Images 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. 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