如何在Bash中将echo分配给变量



我有一个简单的命令:

echo "foo=bar" | cut -d "=" -f1

从一些绳子上剪下一些绳子。

如何将此结果分配给某个变量?

什么都不起作用:

(echo "foo=bar" | cut -d "=" -f1)
$test=`(echo "foo=bar" | cut -d "=" -f1)`
$test=`$(echo "foo=bar" | cut -d "=" -f1)`
$test="$(echo "foo=bar" | cut -d "=" -f1)"

输出:

foo
script.sh: 14: =foo: not found
script.sh: 1: foo: not found
script.sh: 15: =: not found
script.sh: 16: =foo: not found

您可以使用$()(命令替换(:

test=$(echo "foo=bar" | cut -d "=" -f1)
echo "$test"
foo

最新更新