将整数附加到字符串 Bash 脚本 OSX

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9895458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:52:42  来源:igfitidea点击:

Append Integer To String Bash Script OSX

macosbashfor-loop

提问by user1227118

I am attempting to write a bash script that loops exactly 20 times and then appends each number in the loop to the variable with a string "group". The output of each variable should be group1, group2, group3, and so on. Unfortunately i am unable to get this to work. Here is my code thus far:

我正在尝试编写一个 bash 脚本,该脚本正好循环 20 次,然后将循环中的每个数字附加到带有字符串“group”的变量中。每个变量的输出应该是 group1、group2、group3 等等。不幸的是,我无法让它发挥作用。到目前为止,这是我的代码:

   #!/bin/bash

   USERNUM=0
   USERPREFIX='group'

   for (( i = 1 ; i <= 20; i++ ))

   do

   USERNUM=$(($USERNUM+1))

   USERCREATE=$(($USERPREFIX+$USERNUM))

   echo $USERCREATE

   done

I am trying to run this script under OSX 10.6. Any help is greatly appreciated.

我正在尝试在 OSX 10.6 下运行此脚本。任何帮助是极大的赞赏。

回答by Ignacio Vazquez-Abrams

USERCREATE="$USERPREFIX$USERNUM"