从shell脚本中的另一个变量创建变量

时间:2017-06-06 15:30:42

标签: linux bash tail

shell脚本中,我将输出存储为变量,如下所示

Test=$(/home/$USER/import.py $table)

现在这个Test变量将有超过2行,如下所示

table= 123_test
min_id=1
max_id=100
123_test,1,100

现在我想将Test变量的最后一行存储为另一个名为output的变量。

我试过如下。但没有得到理想的结果

output=`$test | tail -n 1`

我们可以从shell脚本中的另一个变量创建变量吗?如果是的话,我们怎么做?

1 个答案:

答案 0 :(得分:1)

使用echo将变量传递给另一个命令。

output=$(echo "$test" | tail -1)

或here-string:

output=$(tail -1 <<<"$test")

不要忘记在两种情况下引用变量,否则将合并所有行。

最后,您可以使用parameter expansion运算符选择最后一行,而不是使用tail

output=${test##*$'\n'}
相关问题