Bash 变量在另一个变量中使用时不显示实际值
我从我试图实现的东西中提取了一段简化代码。从本质上讲,位置变量取自配置文件,并且始终采用Bash 变量在另一个变量中使用时不显示实际值,bash,variables,Bash,Variables,我从我试图实现的东西中提取了一段简化代码。从本质上讲,位置变量取自配置文件,并且始终采用/xyz/$id/abc格式,其中$id表示在服务器上运行的应用程序 代码如下: #!/bin/bash echo "Enter the ID" read id echo echo "Enter the location :" read location echo echo "${id}" echo "${location}" 因此,当我运行代码时: $ ./test.sh Enter the ID x
/xyz/$id/abc
格式,其中$id
表示在服务器上运行的应用程序
代码如下:
#!/bin/bash
echo "Enter the ID"
read id
echo
echo "Enter the location :"
read location
echo
echo "${id}"
echo "${location}"
因此,当我运行代码时:
$ ./test.sh
Enter the ID
xx
Enter the location :
/data/$id/app
xx
/data/$id/app
我试图实现的是,第二个变量根据上述输入打印/data/xx/app
提前感谢您的帮助。
eval
应该会有所帮助:
$ id='123'
$ loc='/data/$id/app'
$ echo "$loc"
/data/$id/app
$ eval echo "$loc"
/data/123/app
请注意,eval
将执行或扩展所有内容,就像您自己键入了$loc
的内容一样。所以你应该注意它的内容。你可以使用一个简单的:
感谢您的回复…前面的答案对我来说更有效,但很高兴知道这个方法。。
id=123
location='/data/$id/app'
location=${location/\$id/$id}
echo $location # /data/123/app