How to Append String to Bash Variable? [2 Effective Ways]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The Bash shell offers a wealth of capabilities for scripting and automation, and one common task is appending strings to variables. Appending strings to a variable means joining a string after the end of another variable using concatenation operator like “+=”.

This article aims to provide a concise yet comprehensive guide on how bash append string to a variable. By the end, you will have the knowledge and techniques to confidently handle string appending, empowering you to optimize your Bash scripts. Let’s get started!

2 Methods to Append String to Variables in Bash Scripts

Appending is one of the most crucial tasks when you work with string. In this section, I will show you two methods to append a string to a Bash variable. The first method is about using ‘+=’ operation and the second method is using ‘${ }’ operator side by side to append a string to variables.

You can read our Comparative Analysis of Methods to distinguish between these two methods and pick the best one for your needs.

1. Append String to Variable Using “+=” Operator

The simplest way to append the string to a variable is to use the addition assignment operator (+=). This operator can concatenate a string to a variable. Here, I will develop a Bash script that will append a string to a variable. Check out the bash script:

#!/bin/bash

var="Hello" #var variable is set

var+=" Good Morning" #string is append to var variable

echo $var #final var variable is printed
EXPLANATION

Firstly, “Hello” is set as the initial value of the var variable. Then += operator has concatenated the “Good morning” text to the initial value of the var variable. Afterward, the echo command prints the final value of the var variable.

appending string to a variable using += operatorThe above image shows that the Bash script has appended a string to a variable.

2. Append String to Variable Using Curly Braces “{}”

Another way to append a string to a Bash variable is to use curly braces {} to concatenate several string variables into a single variable. Here, I have a script that will concatenate three string variables (var1, var2, and var3) into a single variable (var4). To accomplish the same, check the bash script:

#!/bin/bash

#setting var1, var2, var3 variable
var1=Welcome
var2=to
var3=SOFTEKO

#appending var1, var2 and var3 variable on var4 variable
var4="${var1} ${var2} ${var3}"

#printing var4 variable on the terminal
echo $var4
EXPLANATION

At first var1, var2 and var3 variables are defined. Then these three string variables are concatenated on the var4 variable using ${} and then printed on the terminal.

The Bash script has appended three variables on a single variable with ${ } operator and then printed the variable on the terminal.The var_ap2.sh bash script has appended three variables on a single variable and then printed the variable on the terminal.

Comparative Analysis of the Methods to Append String to Bash Variable

Here, I have shown the comparative analysis of the two methods of appending a string to a variable:

Methods Pros Cons
Method 1
  • It is a familiar expression.
  • It is lengthy to write.
Method 2
  • This expression is quite concise
  • Multiple “${ }” expression is used which is supposed to be tiresome.

Above mentioned two methods are user-friendly. You can choose method 1 if you have a predefined variable or method 2 if you want to append a string to a variable instantly.

Conclusion

In conclusion, appending strings to variables in Bash scripts can be done effectively using two methods: the concatenation operator ‘+=’ for simple concatenation and the using ${ } command for more flexibility with formatting and dynamic values. By mastering these techniques, you can enhance your Bash scripting skills and handle string manipulation tasks easily, empowering you to write more robust shell scripts.

People Also Ask

How do you append a string in a variable in bash?

To append a string in a variable in bash, you can use the += operator. Execute the following command sequentially: a="Pi is the number ";  a+="3.14159”; a+=" and it is very handy." and finally echo $a. Output: Pi is the number 3.14159 and it is very handy.

How do you take variables in a string?

To take variables in a string, all you need to do is enclose the variables with curly braces {variable} and place this variable inside the string value, wherever required.

How to concatenate strings in Bash?

To concatenate strings in Bash, you can use += operator. To concatenate string1 and string2, the bash script will be as follows:

string1="Welcome to"
string2=" Linuxsimply.com"
string1+="$string2"
echo "$string1"

The output of this script will be: “Welcome to Linuxsimply.com”.

Can you assign a string to a variable?

Yes, you can assign a string to a variable. The = operator is used to assign a string, which copies the string’s actual bytes from the source operand, up to and including the null byte, to the left-hand variable, which has to be of the string type.

What is the purpose of string?

Strings are mostly used to store text that can be read by humans, such as words and phrases. And information from a computer program is sent to the user of the program via strings.

What is the difference between a variable and a string?

Variables are symbols you may use in a program to store data. So, they may be compared to a blank box that you fill with information or value. But, strings can be used to populate a variable since they are data.


Related Articles


<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial

Rate this post
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment