save the return value of a unix command

Hi all,

I am new in linux script, and I have the code below

#!/bin/bash
CMD="shuf -i 1-5 -n 1"
$CMD
echo $CMD
    exit 0

if I run the $CMD, it will return me a shuffle value. However, I need to save the return value of the shuffle, and use it later on. I tried for example print the return value using $CMD, but instead, it gives me the CMD command, and not the value itself. How can I save the return value of CMD?

Thank you

`...` or $(...) will actually execute the command.
with "..." , you are actually just assigning the literals.

#!/bin/bash
CMD=$(shuf -i 1-5 -n 1)

echo $CMD
    exit 0

PS : welcome to the forum :slight_smile:

1 Like

The built-in variable $? holds the exit status of the most recently executed command.

Thank you all. How do I mark this thread as solved?