在PHP中,字符串按如下方式连接在一起:
$foo = "Hello";
$foo .= " World";
在这里,$foo变成了“Hello World”。
这是如何在Bash中实现的?
在PHP中,字符串按如下方式连接在一起:
$foo = "Hello";
$foo .= " World";
在这里,$foo变成了“Hello World”。
这是如何在Bash中实现的?
当前回答
有人对性能表示担忧,但没有提供数据。让我建议一个简单的测试。
(注意:macOS上的日期不提供纳秒,因此必须在Linux上完成。)
我在GitHub上创建了append_test.sh,内容如下:
#!/bin/bash -e
output(){
ptime=$ctime;
ctime=$(date +%s.%N);
delta=$(bc <<<"$ctime - $ptime");
printf "%2s. %16s chars time: %s delta: %s\n" $n "$(bc <<<"10*(2^$n)")" $ctime $delta;
}
method1(){
echo 'Method: a="$a$a"'
for n in {1..32}; do a="$a$a"; output; done
}
method2(){
echo 'Method: a+="$a"'
for n in {1..32}; do a+="$a"; output; done
}
ctime=0; a="0123456789"; time method$1
测试1:
$ ./append_test.sh 1
Method: a="$a$a"
1. 20 chars time: 1513640431.861671143 delta: 1513640431.861671143
2. 40 chars time: 1513640431.865036344 delta: .003365201
3. 80 chars time: 1513640431.868200952 delta: .003164608
4. 160 chars time: 1513640431.871273553 delta: .003072601
5. 320 chars time: 1513640431.874358253 delta: .003084700
6. 640 chars time: 1513640431.877454625 delta: .003096372
7. 1280 chars time: 1513640431.880551786 delta: .003097161
8. 2560 chars time: 1513640431.883652169 delta: .003100383
9. 5120 chars time: 1513640431.886777451 delta: .003125282
10. 10240 chars time: 1513640431.890066444 delta: .003288993
11. 20480 chars time: 1513640431.893488326 delta: .003421882
12. 40960 chars time: 1513640431.897273327 delta: .003785001
13. 81920 chars time: 1513640431.901740563 delta: .004467236
14. 163840 chars time: 1513640431.907592388 delta: .005851825
15. 327680 chars time: 1513640431.916233664 delta: .008641276
16. 655360 chars time: 1513640431.930577599 delta: .014343935
17. 1310720 chars time: 1513640431.954343112 delta: .023765513
18. 2621440 chars time: 1513640431.999438581 delta: .045095469
19. 5242880 chars time: 1513640432.086792464 delta: .087353883
20. 10485760 chars time: 1513640432.278492932 delta: .191700468
21. 20971520 chars time: 1513640432.672274631 delta: .393781699
22. 41943040 chars time: 1513640433.456406517 delta: .784131886
23. 83886080 chars time: 1513640435.012385162 delta: 1.555978645
24. 167772160 chars time: 1513640438.103865613 delta: 3.091480451
25. 335544320 chars time: 1513640444.267009677 delta: 6.163144064
./append_test.sh: fork: Cannot allocate memory
测试2:
$ ./append_test.sh 2
Method: a+="$a"
1. 20 chars time: 1513640473.460480052 delta: 1513640473.460480052
2. 40 chars time: 1513640473.463738638 delta: .003258586
3. 80 chars time: 1513640473.466868613 delta: .003129975
4. 160 chars time: 1513640473.469948300 delta: .003079687
5. 320 chars time: 1513640473.473001255 delta: .003052955
6. 640 chars time: 1513640473.476086165 delta: .003084910
7. 1280 chars time: 1513640473.479196664 delta: .003110499
8. 2560 chars time: 1513640473.482355769 delta: .003159105
9. 5120 chars time: 1513640473.485495401 delta: .003139632
10. 10240 chars time: 1513640473.488655040 delta: .003159639
11. 20480 chars time: 1513640473.491946159 delta: .003291119
12. 40960 chars time: 1513640473.495354094 delta: .003407935
13. 81920 chars time: 1513640473.499138230 delta: .003784136
14. 163840 chars time: 1513640473.503646917 delta: .004508687
15. 327680 chars time: 1513640473.509647651 delta: .006000734
16. 655360 chars time: 1513640473.518517787 delta: .008870136
17. 1310720 chars time: 1513640473.533228130 delta: .014710343
18. 2621440 chars time: 1513640473.560111613 delta: .026883483
19. 5242880 chars time: 1513640473.606959569 delta: .046847956
20. 10485760 chars time: 1513640473.699051712 delta: .092092143
21. 20971520 chars time: 1513640473.898097661 delta: .199045949
22. 41943040 chars time: 1513640474.299620758 delta: .401523097
23. 83886080 chars time: 1513640475.092311556 delta: .792690798
24. 167772160 chars time: 1513640476.660698221 delta: 1.568386665
25. 335544320 chars time: 1513640479.776806227 delta: 3.116108006
./append_test.sh: fork: Cannot allocate memory
这些错误表明,我的Bash在崩溃之前达到了335.54432MB。您可以将代码从双倍数据更改为附加一个常量,以获得更精细的图形和故障点。但我认为这应该给你足够的信息来决定你是否在乎。就个人而言,低于100 MB我不会。您的里程数可能有所不同。
其他回答
我有点喜欢做一个快速的功能。
#! /bin/sh -f
function combo() {
echo $@
}
echo $(combo 'foo''bar')
还有一种剥猫皮的方法。这一次有功能:D
在我看来,连接两个字符串的最简单方法是编写一个函数,然后使用该函数。
function concat ()
{
prefix=$1
suffix=$2
echo "${prefix}${suffix}"
}
foo="Super"
bar="man"
concat $foo $bar # Superman
alien=$(concat $foo $bar)
echo $alien # Superman
如果这是将“World”添加到原始字符串的示例,则可以是:
#!/bin/bash
foo="Hello"
foo=$foo" World"
echo $foo
输出:
Hello World
我更喜欢使用花括号${}来扩展字符串中的变量:
foo="Hello"
foo="${foo} World"
echo $foo
> Hello World
卷曲括号将适合连续字符串用法:
foo="Hello"
foo="${foo}World"
echo $foo
> HelloWorld
否则,使用foo=“$fooWorld”将不起作用。
这里是对大多数答案的简要总结。
假设我们有两个变量,$1设置为“1”:
set one two
a=hello
b=world
下表说明了我们可以组合a和b的值来创建新变量c的不同上下文。
Context | Expression | Result (value of c)
--------------------------------------+-----------------------+---------------------
Two variables | c=$a$b | helloworld
A variable and a literal | c=${a}_world | hello_world
A variable and a literal | c=$1world | oneworld
A variable and a literal | c=$a/world | hello/world
A variable, a literal, with a space | c=${a}" world" | hello world
A more complex expression | c="${a}_one|${b}_2" | hello_one|world_2
Using += operator (Bash 3.1 or later) | c=$a; c+=$b | helloworld
Append literal with += | c=$a; c+=" world" | hello world
几点注意事项
将赋值的RHS括在双引号中通常是一种很好的做法,尽管在许多情况下它是可选的+如果以小增量构造大字符串,特别是在循环中,则从性能角度来看,=更好在变量名周围使用{}来消除扩展的歧义(如上表第2行所示)。如第3行和第4行所示,不需要{},除非变量与一个字符串连接,该字符串以shell变量名中有效的第一个字符(即字母或下划线)开头。
另请参见:
BashFAQ/013-如何连接两个变量?我们什么时候需要在外壳变量周围使用大括号?