Bash (Bourne Again SHell) scripting is renowned for its versatility and power in automation tasks. Effective string manipulation within Bash variables is key to unlocking its full potential. This concise guide explores the fundamentals of Bash variables and delves into essential techniques for handling strings. Gain the knowledge and skill needed to manipulate, concatenate, substitute, and interpolate strings with ease. Let’s embark on a journey to master the art of Bash use variable in string.
Key Takeaways
- Getting familiar with the concept of the Bash variables.
- Knowing the process of incorporating string on the Bash variable.
Free Downloads
4 Practical Cases Related to String Variables in Bash Scripts
The string is a powerful variable of the Bash script. To assign a string on a variable, follow the below syntax
variable_name="string"
Here, I have discussed some cases related to the Bash string variables.
Case 01: Print a String Variable Using Bash Script
You can store strings in the Bash script variables. In this example, I will develop a Bash script in which I will set a string in a Bash variable, then print the variable value on the terminal. Follow the below process to do so.
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a file in Nano:
nano string.sh
- nano: Opens the nano text editor.
- string.sh: Bash script file name.
❸ Copy the script mentioned below:
#!/bin/bash
var_1="LinuxSimply" #setting a string on the var_1 variable
echo $var_1 #prints the value of the var_1 variable
#! /bin/bash ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script; in this case, it’s bash. Then the var_1=”LinuxSimply” command set a string as the value of the var_1 variable. Finally, the echo $var_1 prints the value of the var_1 variable.
❹ Press CTRL+O and ENTER to save the file; CTRL+X to exit.
➎ Run the script by using the following command:
bash string.sh
- bash: Executes the bash scripts.
- string.sh: Bash script file name.
The image shows that the bash script has printed the value of a string variable on the terminal.
Case 02: Concatenating String Variables in Bash Script
The programmers may need to concatenate two strings inside a variable and print them on the terminal. In this example, I will develop a bash script to concatenate two variables and then print them on the terminal. To do so, follow the below Bash script.
You can follow the steps of case 01, to save & make the script executable.
Script (strcat.sh) >
#! /bin/bash
a="Happy" #setting string on the a variable
b="Scripting" #setting string on the b variable
echo "${a}${b}" #concatenating a and b then print them on the terminal
The a=”Happy” and b=”Scripting” commands set strings as the value of the a and the b variable, then the echo “${a}${b}” command concatenate a and b then print them on the terminal.
Run the script by executing the following command:
bash strcat.sh
The above image shows that the Bash script has concatenated two variables and printed them on the terminal.
Case 03: Concatenating a Variable and a String
Sometimes programmers may need to concatenate strings with variables. In this example, I will develop a script that will concatenate strings with variables. To do so, follow the below script.
You can follow the steps of case 01, to save & make the script executable.
Script (concat.sh) >
#!/bin/bash
var="Good" #setting var variable
echo "$var Morning" #concatenating string with variable then printing
The var=”Good” command sets a value to the var variable. Then the echo “$var Morning” command concatenates the string with the var variable and then prints on the terminal.
Run the script by using the following command:
bash concat.sh
The above image illustrates that the bash script has concatenated a variable with a string and then printed its value.
Case 04: Concatenating an Integer and a String Variable
The programmers may often need to work with numeric data on the Bash variable. To ease such situations, I have developed a bash script that works with numeric data of Bash variables.
You can follow the steps of case 01, to save & make the script executable.
Script (numeric.sh) >
#!/bin/bash
n=130 #setting the value of n 130
((n=n+20)) #add 20 with n and keep the value to n
echo $n #print the value of n on the terminal
The n=130 command sets 130 as the value of n. Then the ((n=n+20)) command adds 20 with n and keeps the value to n. Finally, the echo $n command prints the value of n on the terminal
Run the script by executing the following command:
bash numeric.sh
The above image shows that the bash script has handled a numeric variable and done an arithmetic operation, then printed the variable value on the terminal.
Conclusion
In conclusion, mastering string variables in Bash is essential for effective scripting. By understanding the assignment, concatenation, interpolation, and manipulation techniques, you can enhance script flexibility and solve problems efficiently. Embrace the power of strings in Bash and unleash the potential of your scripts.