linux bash 字符串 连接,Bash连接字符串

在本小节中,将学习如何在Bash Shell脚本中添加或连接字符串。

在bash脚本编制中,可以将两个或多个字符串添加或连接在一起,这称为字符串连接。它是任何一种编程语言的通用要求之一。应用特殊字符或内置函数来执行字符串连接。但是,Bash不包含任何内置函数来组合字符串数据或变量。在bash中执行字符串连接的最简单方法是并排写入变量。

例如,假设有两个字符串(即"Welcome"和"to Yiibai"),要将这两个字符串连接在一起,然后创建了一个新字符串("Welcome to Yiibai"),这种概念称为字符串连接。

语法命令

用于连接字符串的命令定义为:

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

执行上面示例代码,得到以下结果:

48a9f15b7708854159acda8102ca2f36.png

示例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"

执行上面示例代码,得到以下结果:

f3880d92e0e377a290a6535b640721f0.png

字符串连接是编程语言中生成有意义的输出所必需的功能之一。本小节中介绍了在bash中连接字符串的几种常见的方法。

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值