In a shell
script I will store the output as a variable like below
Test=$(/home/$USER/import.py $table)
Now this Test
variable will have more than 2 lines like below
table= 123_test
min_id=1
max_id=100
123_test,1,100
Now I want to store the last line of the Test
variable as another variable called output
.
I have tried like below. But not getting the desired result
output=`$test | tail -n 1`
Can we create a variable from another variable in a shell
script. If yes, How can we do that?
Use echo
to pipe the variable to the other command.
output=$(echo "$test" | tail -1)
or a here-string:
output=$(tail -1 <<<"$test")
Don't forget to quote the variable in both cases, otherwise all the lines will be combined.
Finally, you can use a parameter expansion operator to select the last line, instead of using tail
:
output=${test##*$'\n'}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.