Bash 修改一个变量

Bash Modifying a variable

我有一个正在执行的命令,这个命令存储在某个变量中,而存储这个命令的这个变量存储另一个变量 问题是当我改变我的第二个变量时,它并没有在我的第一个变量中动态改变,所以执行的命令总是使用相同的变量。 这是我的代码:

test="test"
TEMP_STR="doesn't exist"
checkClient=`p4 client -o -t $test 2>&1`
echo this is the output: "$checkClient"
test="${test}_2"
echo echo this is the new client name "$test"
echo this is the new output: "$checkClient"

这是输出:

this is the output: Client 'test' doesn't exist.
echo this is the new client name test_2
this is the new output: Client 'test' doesn't exist.

知道如何解决这个问题吗?

根据您的描述和评论,您似乎喜欢这样的行为,即每当变量发生变化时,您希望相关命令根据变量的新值 return 不同的值。

您实际上可以为此使用 BASH 函数

checkClient() { p4 client -o -t "$arg" 2>&1; }

现在用作:

arg="test"   
echo "this is the output: $(checkClient)"

arg="${test}_2"
echo echo this is the new client name "$arg"
echo "this is the new output: $(checkClient)"