If I have a subshell command:
output="$(runfoo)";
is there way to store only the last line of the output from runfoo
into the variable output
? Or perhaps only the first line?
If I have a subshell command:
output="$(runfoo)";
is there way to store only the last line of the output from runfoo
into the variable output
? Or perhaps only the first line?
Only stdout:
output="$(runfoo | tail -n 1)"
output="$(runfoo | head -n 1)"
Stdout and stderr:
output="$(runfoo 2>&1 | tail -n 1)"
output="$(runfoo 2>&1 | head -n 1)"
With bash
IFS=$'\n' output=$(inter=($(runfoo))
printf '%s\n' "${inter[0]}" "${inter[((${#inter[@]}-1))]}")
echo "$output"
runfoo return a multiline result like that :
first aaa
xcv
pattern3 a
bbb
last qqq
inter is a array (inter for intermediary)
inter=($(runfoo)) get the result of the runfoo command '$(runfoo)' in the array inter=($(...))
This way,
inter[0]=first
inter[1]=aaa
inter[6]=bbb
because IFS= whitespace tab or newline
So setting IFS to newline at the start
Each item of the array inter is a line of text
inter[0]=first aaa
inter[2]=pattern 3
output=$(create a array and print the first and the last item of this array)
So echo "$output"
first aaa
last qqq
Hope this help.