在本小節中,將學習如何在Bash Shell腳本中添加或連接字符串。
linux useradd,在bash腳本編制中,可以將兩個或多個字符串添加或連接在一起,這稱為字符串連接。它是任何一種編程語言的通用要求之一。應用特殊字符或內置函數來執行字符串連接。但是,Bash不包含任何內置函數來組合字符串數據或變量。在bash中執行字符串連接的最簡單方法是并排寫入變量。
例如,假設有兩個字符串(即"Welcome"和"to Yiibai"),要將這兩個字符串連接在一起,然后創建了一個新字符串("Welcome to Yiibai"),這種概念稱為字符串連接。
linux grep?語法命令
用于連接字符串的命令定義為:
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 Yiibai."
#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 Yiibai."
執行上面示例代碼,得到以下結果:
maxsu@yiibai:~/bashcode$ cat /dev/null > concat-string.sh
maxsu@yiibai:~/bashcode$ vi concat-string.sh
maxsu@yiibai:~/bashcode$ ./concat-string.sh
We welcome you on Yiibai.
示例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@yiibai:~/bashcode$ cat /dev/null > concat-string.sh
maxsu@yiibai:~/bashcode$ vi concat-string.sh
maxsu@yiibai:~/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 Yiibai."
echo $new_str
執行上面示例代碼,得到以下結果:
maxsu@yiibai:~/bashcode$ cat /dev/null > concat-string.sh
maxsu@yiibai:~/bashcode$ vi concat-string.sh
maxsu@yiibai:~/bashcode$ ./concat-string.sh
Welcome to Yiibai.
示例5:使用文字字符串連接
字符串連接也可以通過大括號{}與文字字符串一起執行,使用應避免變量與文字字符串混淆。
Bash腳本
#!/bin/bash
str="Welcome to"
newstr="${str} Yiibai."
echo "$newstr"
執行上面示例代碼,得到以下結果:
maxsu@yiibai:~/bashcode$ cat /dev/null > concat-string.sh
maxsu@yiibai:~/bashcode$ vi concat-string.sh
maxsu@yiibai:~/bashcode$ ./concat-string.sh
Welcome to Yiibai.
示例6:使用下劃線連接
使用下劃線在bash shell中連接字符串是常見的任務之一,它主要用于為文件分配名稱。
Bash腳本
#!/bin/bash
str1="Hello"
str2="World!"
echo "${str1}_${str2}"
執行上面示例代碼,得到以下結果:
maxsu@yiibai:~/bashcode$ cat /dev/null > concat-string.sh
maxsu@yiibai:~/bashcode$ vi concat-string.sh
maxsu@yiibai:~/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中連接字符串的幾種常見的方法。
¥ 我要打賞
糾錯/補充
收藏
加QQ群啦,易百教程官方技術學習群
注意:建議每個人選自己的技術方向加群,同一個QQ最多限加 3 個群。
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态