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

The Bash shell offers a wealth of capabilities for scripting and automation, and one common task is appending strings to variables. This article aims to provide a concise yet comprehensive guide on how bash append string to variable. By the end, you will have the knowledge and techniques to confidently handle string appending, empowering you to optimize your Bash scripts and streamline your workflows. Let’s get started!

Key Takeaways

  • Knowing about the += operator to append the string to the Bash variables.
  • Learning concatenation to append the string to the Bash variables.

Free Downloads

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.

Method 01: Using the “+=” Operator to Append String to Variable

The simplest way to append the string to a variable is to use the += operator. Here, I will develop a Bash script that will append a string to a variable. To do so, follow the below procedures.

Steps to Follow >

➊ Open the Ubuntu terminal.

➋ Execute the following command into the terminal to create a file named var_ap.sh file.

nano var_ap.sh
EXPLANATION
  • nano: Opens and creates a file in the nano text editor.
  • var_ap.sh: File name.

➌ Now, copy the following command to the text editor.

#!/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

The code starts with #!/bin/bash, which specifies the Bash interpreter. Then “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.

➍ To save and exit the text editor press CTRL+ O and CTRL+X.

➎ Finally, you can simply run the file from your command line by executing:

bash var_ap.sh
EXPLANATION
  • bash: Executes the bash scripts.
  • var_ap.sh: Bash script name.

The Bash script has appended a string to a variable with += operator.The above image shows that the Bash script has appended a string to a variable.

Method 02: Using Concatenation to Appeng String to Variable

Another way to append a string to a Bash variable is to concatenate several variables into a single variable. Here, I have a script that will concatenate three variables (var1, var2, and var3) into a single variable (var4). To accomplish the same, follow the below script.

You can follow the steps mentioned in method 1 to know how to write, execute and run the bash script.

Script (var_ap2.sh) >

#!/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 variables are concatenated on the var4 variable then printed on the terminal.

Finally, you can simply run the file from your command line by executing:

bash var_ap2.sh

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

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?
You can use the += operator to append to the end of a string variable. 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?
All you need to do is enclose the variables with curly braces {variable} and place this variable inside the string value, wherever required.
Can you 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. Hence, strings can be used to populate a variable since they are data.
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