在本小節中,將學習如何在Bash Shell腳本中添加或連接字串。
在bash腳本編制中,可以將兩個或多個字串添加或連接在一起,這稱為字串連接。它是任何一種編程語言的通用要求之一。應用特殊字元或內置函數來執行字串連接。但是,Bash不包含任何內置函數來組合字串數據或變數。在bash中執行字串連接的最簡單方法是並排寫入變數。
例如,假設有兩個字串(即"Welcome"
和"to zaixian"
),要將這兩個字串連接在一起,然後創建了一個新字串("Welcome to zaixian"
),這種概念稱為字串連接。
語法命令
用於連接字串的命令定義為:
str3="$str1$str2"
注意:遵守上面的命令; 賦值(
=
)運算符之前或之後不應有任何空格。str
用於指示字串。
此命令將串聯str1
和str2
變數的值,並將其存儲在第三個變數str3
中。
以下是一些說明了字串連接的不同方式的示例:
示例1:並排寫入變數連接
這是字串連接的基本示例,並且在此方法中不需要其他運算符或函數。
Bash腳本
#!/bin/bash
#Script to Concatenate Strings
#Declaring the first String
str1="We welcome you"
#Declaring the Second String
str2=" on zaixian."
#Combining first and second string
str3="$str1$str2"
#Printing a new string by combining both
echo $str3
執行上面示例代碼,得到以下結果:
示例2:使用雙引號連接
另一個方法是在字串中使用雙引號定義的變數。字串變數可以應用於字串數據的任何位置。
Bash腳本
#!/bin/bash
#Script to Concatenate Strings
#Declaring String Variable
str="We welcome you"
#Add the variable within the string
echo "$str on zaixian."
執行上面示例代碼,得到以下結果:
maxsu@zaixian:~/bashcode$ cat /dev/null > concat-string.sh
maxsu@zaixian:~/bashcode$ vi concat-string.sh
maxsu@zaixian:~/bashcode$ ./concat-string.sh
We welcome you on zaixian.
示例3:將追加運算符與迴圈一起使用連接
大多數流行的編程語言都支持追加運算符(+=
),它是加號和等號的組合。它將新的字串添加到字串變數的末尾。
Bash腳本
#!/bin/bash
echo "Printing the name of the programming languages"
#Initializing the variable before combining
lang=""
#for loop for reading the list
for value in 'java' 'python' 'C' 'C++' 'Bash';
do
lang+="$value " #Combining the list values using append operator
done
#Printing the combined values
echo "$lang"
執行上面示例代碼,得到以下結果:
maxsu@zaixian:~/bashcode$ cat /dev/null > concat-string.sh
maxsu@zaixian:~/bashcode$ vi concat-string.sh
maxsu@zaixian:~/bashcode$ ./concat-string.sh
Printing the name of the programming languages
java python C C++ Bash
示例4:使用Printf函數連接
在bash中,printf
是用於列印和連接字串的函數。
Bash腳本
#!/bin/bash
str="Welcome"
printf -v new_str "$str to zaixian."
echo $new_str
執行上面示例代碼,得到以下結果:
maxsu@zaixian:~/bashcode$ cat /dev/null > concat-string.sh
maxsu@zaixian:~/bashcode$ vi concat-string.sh
maxsu@zaixian:~/bashcode$ ./concat-string.sh
Welcome to zaixian.
示例5:使用文字字串連接
字串連接也可以通過大括弧{}
與文字字串一起執行,使用應避免變數與文字字串混淆。
Bash腳本
#!/bin/bash
str="Welcome to"
newstr="${str} zaixian."
echo "$newstr"
執行上面示例代碼,得到以下結果:
maxsu@zaixian:~/bashcode$ cat /dev/null > concat-string.sh
maxsu@zaixian:~/bashcode$ vi concat-string.sh
maxsu@zaixian:~/bashcode$ ./concat-string.sh
Welcome to zaixian.
示例6:使用下劃線連接
使用下劃線在bash shell中連接字串是常見的任務之一,它主要用於為檔分配名稱。
Bash腳本
#!/bin/bash
str1="Hello"
str2="World!"
echo "${str1}_${str2}"
執行上面示例代碼,得到以下結果:
maxsu@zaixian:~/bashcode$ cat /dev/null > concat-string.sh
maxsu@zaixian:~/bashcode$ vi concat-string.sh
maxsu@zaixian:~/bashcode$ ./concat-string.sh
Hello_World!
示例7:使用任意字元連接
Bash腳本
#!/bin/bash
#String Concatenation by Character (,) with User Input
read -p "Enter First Name: " name
read -p "Enter City: " state
read -p "Enter Age: " age
combine="$name,$state,$age"
echo "Name, City, Age: $combine"
執行上面示例代碼,得到以下結果:
字串連接是編程語言中生成有意義的輸出所必需的功能之一。本小節仲介紹了在bash中連接字串的幾種常見的方法。