How to Use String Variables in Bash Script? [4 Cases]

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.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a file in Nano:

nano string.sh
EXPLANATION
  • 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
EXPLANATION

#! /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
EXPLANATION
  • bash: Executes the bash scripts.
  • string.sh: Bash script file name.

The bash script has printed the value of a string variable on the terminal.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
EXPLANATION

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 Bash script has concatenated two variables and printed them on the terminal.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
EXPLANATION

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 bash script has concatenated a variable with a string and then printed its value.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
EXPLANATION

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 bash script has handled a numeric variable and done an arithmetic operation, then printed the variable value on the terminal.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.

People Also Ask

How to use a variable in a string bash?
Bash script can print out a variable whenever a user requests it. To put it differently, you must use a $ sign before its name. Bash will be informed that the user intends to utilize a variable value in this manner. The $ symbol is not required to establish a variable or change its value; just the variable’s actual name is required.
Are all variables stored as strings in bash?
Contrary to many other programming languages, Bash does not divide its variables into categories based on “type.” Bash variables are just character strings. However, depending on the situation, Bash also allows for arithmetic and comparisons of variables. The key criterion is whether a variable’s value is made up entirely of digits.
How do I set a variable equal to a string in bash?
The ” = ” and ” == ” operators can be used in bash to compare strings. The distance between the brackets is crucial. In the right way, operators should have space surrounding them, and brackets should have space inside them.
How do you compare strings in bash?
In Bash, the following operators can be used to compare strings, string1 = string2, string1 == string2, If the operands are equal, the equality operator returns true.
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