linux shell 字符串拼接方法

linux shell 字符串拼接方法,第1张

CROSS_COMPILE=arm-openwrt-linux-muslgnueabi-

echo $CROSS_COMPILE

方法1:

    test1=$(echo $CROSS_COMPILE)gcc

    echo $test1

方法2:

    test2=${CROSS_COMPILE}gccabc

    echo $test2

这篇文章主要介绍了Linux

shell脚本中连接字符串的方法,大家参考使用吧

如果想要在变量后面添加一个字符,可以用一下方法:

代码如下:

$value1=home

$value2=${value1}"="

echo

$value2

把要添加的字符串变量添加{},并且需要把$放到外面。

这样输出的结果是:home=,也就是说连接成功。

又如:

代码如下:

[root@localhost

sh]#

var1=/etc/

[root@localhost

sh]#

var2=yum.repos.d/

[root@localhost

sh]#

var3=${var1}${var2}

[root@localhost

sh]#

echo

$var3

/etc/yum.repos.d/

可以使用strcat()函数,函数原型char *strcat(char *dest,const char *src)函数说明:strcat()会将参数src字符串拷贝到参数dest所指的字符串尾。第一个参数dest要有足够的空间来容纳要拷贝的字符串。返回值:返回参数dest的字符串起始地址。

例子:

#include<string.h>

main()

{

char a[30]="string1"

char b[]="string2"

printf("before strcat():%s\n",a)

printf("after strcat():%s\n",strcat(a,b))

}

执行结果:

before strcat():string1

after strcat():string1string2


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8475181.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-16
下一篇2023-04-16

发表评论

登录后才能评论

评论列表(0条)

    保存