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

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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 using variables in string.

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, and then print the variable value on the terminal.

To print a string variable in a bash script, follow the script described below:

  1. First, launch an Ubuntu Terminal.
  2. 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.
  3. 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 sets a string as the value of the var_1 variable. Finally, the echo $var_1 prints the value of the var_1 variable.

  4. Press CTRL+O and ENTER to save the file; CTRL+X to exit.
  5. 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.

To concatenate two variables and then print them on the terminal, use the following script:

#! /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 concatenates a and b and then prints 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. Place the variables and strings within double quotes, and use the $ symbol followed by the variable name to embed its value within the concatenated string.

To concatenate strings with variables, follow the below script:

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

Concatenating an integer and a string variable in Bash involves using the variable names within a string, preferably enclosed in curly braces preceded by a dollar sign to ensure variable substitution. Here’s an example:

#!/bin/bash

#read the numeric value from the user
read -p "Enter a numerical value: " integer_variable
string_variable="The given numeric value is"

#Concatenating the number and string value
result="${string_variable} ${integer_variable}"

#printing the value to the command line
echo $result
EXPLANATION

In this example, the variable result is created by concatenating the string “The given numeric value is” with the value of the integer variable integer_variable using the ${} syntax. Lastly, the output of the result variable has been displayed using the echo command.

Run the script by executing the following command:

./string_integer.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 concatenated a numeric value with a string and displays “The given numeric value is 122” upon receiving 112 as user input to the command line.

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?

In Bash, you can use a variable in a string by enclosing the variable name within curly braces preceded by a dollar sign. For example, if you have a variable named name, you can include it in a string like this: echo "Hello, ${name}!". The curly braces help to delimit the variable name from surrounding text and ensure accurate variable substitution within the string.

Are all variables stored as strings in bash?

No, not all variables in Bash are stored as strings. Bash supports different types of variables, including integers and arrays. When you assign a value to a variable, Bash automatically interprets the type based on the context.

For example, if you assign a sequence of characters (e.g., “123”) to a variable, Bash treats it as a string. However, if you assign a numerical value (e.g., 123) to a variable, Bash interprets it as an integer.

How do I set a variable equal to a string in bash?

To set a variable equal to a string in Bash, you employ the syntax variable_name="your_string_here". This assigns the specified string to the variable named variable_name. Note that the absence of spaces around the equal sign is crucial to prevent syntax errors.

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.


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