How to I read the second line of the output of a command into a bash variable?

bash

Solution

With `sed`:

var=$(echo -e "AAA\nBBB" | sed -n '2p')

With `awk`:

var=$(echo -e "AAA\nBBB" | awk 'NR==2')

Then simply, echo your variable:

echo "$var"

Problem

I have a command that prints several lines and I do want to put the second line into a bash variable. like `echo "AAA\nBBB"` and I want a bash command that would put `BBB` in a bash variable.

Original source