Linux Bash修改变量

Linux Bash修改变量,linux,bash,unix,Linux,Bash,Unix,我有一个正在执行的命令,这个命令存储在某个变量中,而这个存储这个命令的变量存储另一个变量 问题是,当我改变我的第二个变量时,它不会在我的第一个变量中动态改变,所以执行的命令总是使用相同的变量。 这是我的密码: test="test" TEMP_STR="doesn't exist" checkClient=`p4 client -o -t $test 2>&1` echo this is the output: "$checkClient" test="${test}_2" ech

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

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.

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

根据您描述和评论的方式,似乎您希望有这样一种行为,即每当变量发生变化时,您希望相关命令根据变量的新值返回不同的值

实际上,您可以使用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)"

您从未再次运行过该命令。我在第二次
echo
checkClient=`p4 client-o-t$test 2>&1`
中使用此命令。您不是在此处存储命令,而是在执行命令并存储其输出。否。您正在第二个echo中使用第一个命令的输出。反勾号运行该命令。它们不存储要通过变量运行的命令。您希望每次更改$test时$checkClient都会更改吗?