SoFunction
Updated on 2025-03-02

Linux Shell string variable stitching and assignment use

Recently, I have used shell scripts in my work and used string variable splicing. At the same time, I need to assign values ​​to the strings. I will share with you here.

1. String stitching

The string can be spliced ​​in the shell script, and the spliced ​​value can be assigned to another variable. The following figure shows several examples of string splicing for your reference.

#!/bin/bash
a="123"  # Define a variable a to "123"b="456"  # Define the b variable to "456"first=$a$b  # The first way to splice strings: take 2 values ​​directly and splice the variable valuesecho $first
second="$a$b" # The second way to splice strings: add strings to the outermost part of the two variable valuesecho $second
third="${a}${b}" # The third method of splicing strings: similar to the second method, is to add ${} to get the value of the variable.echo $third

Execute the above script and you can see the execution results of the script. The results of these three methods are the same. The above three types can be spliced ​​with strings:

123456
123456
123456

2. Define strings with double quotes or single quotes

We know that single quotes and double quotes have special meanings in shells, and there are differences between them, but how can we define a string so that the value of the string itself contains double quotes or single quotes? In the following code, we give an example:

#!/bin/bash
a="\"\""
b="'''"
c='""'
d=''''
echo $a
echo $b
echo $c
echo $d

Execute the above script and we can see the execution result of the script:

"" # The value of variable a is 2 double quotes
‘’’ # The value of variable b is 3 single quotes
"" # The value of variable c is 2 single quotes
# The value of variable d is empty

After seeing the above execution results, we roughly understand how to define a string with double quotes or single quotes. Let’s share the technical knowledge points I have summarized:

There are two ways to define strings with double quotes:
1.1 When defining a variable, use double quotes on the outermost layer and \ inside the double quotes to escape double quotes, similar to how the variable a is defined.
1.2 When defining a variable, use single quotes on the outermost layer, and write double quotes directly inside the single quotes to define it, similar to the definition method of variable c.
A string method with a value of single quotes: Use double quotes on the outermost layer, and write single quotes directly inside the double quotes to define, similar to the definition of the variable b.

The above are just some of the knowledge points I have summarized. There must be omissions or other methods. You are welcome to add them. In fact, as for why the above situation occurs, I personally think that the fundamental reason is that the functions of single and double quotes in shell scripts are different. , interested people can Baidu on their own. It is the difference between the two that leads to the above situation.

3. Take variable values ​​in single and double quote strings

The difference between using single quotes and double quotes in shells leads to the difference in the values ​​taken in single quotes and double quotes variables. Let’s first look at an example to explain the differences between the two:

#!/bin/bash
name="test"
a='{
  "name":$name,
  "age":"45"
}'
b="{
  "name":$name,
  "age":"45"
}"
echo $a
echo $b

Execute the above shell script and you can see the output result:

{ "name":$name, "age":"45" } # This is the value of a
{ name:test, age:45 } # This is the value of b

Through the above execution results, we can see the following questions:

  • The only difference between the definition of variable a and the definition of variable b is that a is single quotes and b is double quotes.
  • The value of variable a does not take out the value of name variable, and variable b takes out the value of name.
  • The attribute names defined in variable a are all with double quotes (for example: "name"), and the attribute names defined by the value of variable b are not with double quotes (for example: name)

So how to solve the second and third problems mentioned above? First, let’s look at the second problem. Here is a solution for me:

In a string defined by single quotes, if you want to get the value of a variable, you need to add a layer of single quotes to the variable value.

Below we make a modification to the previous definition of variable a and give an example to illustrate the result.

#!/bin/bash
name="test"
a='{
  "name":'$name',   ---> Here foraAdd a layer of single quotes to the definition
  "age":"45"
}'
b="{
  "name":$name,
  "age":"45"
}"
echo $a
echo $b

Execute the above result again and we can see the execution result:

{ "name":test, "age":"45" }
{ name:test, age:45 }

You can see the result of the execution. In variable a, the value of variable name is indeed taken out and the value has been assigned.

Next, let’s look at the third question, which is how to add double quotes to the attribute name of the variable b. The solution to this problem is similar to how to define a string with a value of double quotes or single quotes in our second section. Here is a solution for me: use escape characters. Here is my example:

name="test"
a='{
  "name":'$name',   ---> Here foraAdd a layer of single quotes to the definition
  "age":"45"
}'
b="{
  "\"name\"":$name, ---> Add escape characters to double quotes for each property value
   "\"age\"":"\"45\""
}"
echo $a
echo $b

Execute the above results, we can see that the execution result is:

{ "name":test, "age":"45" }
{ "name":test, "age":"45" }

You can see that the values ​​of the two are consistent, and we can draw the following conclusion:

  • Although the two implement the same functions, they find that the definition of using double quotes is more cumbersome, and the method of using single quotes is easier.
  • When using single quote definitions, be careful to take the value of a variable, add single quotes to the variable value to get the variable value.
  • When using double quote definitions, if you want to display double quotes normally, you need to use escape characters to define double quotes.

This is the article about the use of Linux Shell string variable splicing and assignment. For more related contents of Shell string variable splicing and assignment, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!