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.
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.
➊ Open the Ubuntu terminal.
➋ Execute the following command into the terminal to create a file named var_ap.sh file.
nano var_ap.sh
- 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
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
- bash: Executes the bash scripts.
- var_ap.sh: Bash script name.
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.
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
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 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 |
|
|
Method 2 |
|
|
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.