Shilpi Agrawal Shilpi Agrawal - 1 year ago 45
Bash Question

Store the output of shell command in a variable

I am trying to install zfs through shell script while installation I am getting some error so, to automate it fully, I want to get the version to be installed from error itself. For all other commands I am getting the error in one variable but for one command its not coming at all. I tried every solution possible.

I need output of this command

sploutput=$(sudo dkms install -m spl -v $version)

echo $sploutput

echo $sploutput # This is giving nothing.

I tried wrapping it around string also like
"sploutput=$(sudo dkms install -m spl -v $version)"

echo "{sploutput}"

Nothing seems to work.

zmo zmo
Answer Source
sploutput=$(sudo dkms install -m spl -v $version)
echo $sploutput

it might be because the dkms install outputs on STDERR and not on STDOUT, and with the command you're using, you're only getting STDOUT output in the variable. To take both, you can try:

sploutput=$(sudo dkms install -m spl -v $version 2>&1)

to redirect STDERR into STDOUT.