gpt4 book ai didi

linux - bash Linux 中的编号字符串

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:25 25 4
gpt4 key购买 nike

我想在 bash 中定义连续编号的字符串,如下所示:

String1
String2
String3
String4
String5

到目前为止,我所做的如下,但效果不佳。

#!/bin/bash
str_name='String'
for i in `seq 1 5`
do
expr=$(($str_name + $i)) #this part is what I cannot deal with it.
echo $expr
done

谢谢

最佳答案

您所做的是在算术意义上添加变量。您要使用以下内容:

expr="${str_name}${i}"

注意:变量不需要用方括号括起来。我这样做只是因为它有时更容易阅读,它还可以防止我犯愚蠢的错误。

更详细地说,$(...) 在子 shell 中执行括号之间的任何操作并返回结果。我不确定回显 $expr 时你得到了什么输出,但我认为这是一条错误消息。像 Command String + 1 找不到 但我真的不知道。

关于linux - bash Linux 中的编号字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37933297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com