Bash 变量$1作为变量
如何将$1变量设置为另一个变量?我想运行一些类似于:Bash 变量$1作为变量,bash,Bash,如何将$1变量设置为另一个变量?我想运行一些类似于: $ ./script london Some output with John in text 脚本应该是这样的: #!/bin/bash london="John" ukraina="Yury" romania="Ion" cat /path/to/file | grep $1 我不建议盲目地查找变量名。用户可以传入任何变量,而不仅仅是您期望的三个变量中的一个 相反,请使用关联数组 declare -A names=([london]=
$ ./script london
Some output with John in text
脚本应该是这样的:
#!/bin/bash
london="John"
ukraina="Yury"
romania="Ion"
cat /path/to/file | grep $1
我不建议盲目地查找变量名。用户可以传入任何变量,而不仅仅是您期望的三个变量中的一个 相反,请使用关联数组
declare -A names=([london]=John [ukraina]=Yury [romania]=Ion)
grep "${names[$1]}" /path/to/file
或者,如果您的bash版本不支持关联数组,请使用大小写查找
如果要将$1声明为变量,请使用以下命令:
VARIABLE="$1"
例如:
#!/bin/bash
VARIABLE="$1"
echo $VARIABLE
VARIABLE="$1"
#!/bin/bash
VARIABLE="$1"
echo $VARIABLE