Search code examples
linuxbashrhel

Linux Bash Script Variable as command


I have to create a script on RHEL and was wondering if I am able to save the output of a command as a variable. For example:

4.2.2 Ensure logging is configured (Not Scored) : = OUTPUT (which comes from the command sudo cat /etc/rsyslog.conf)

This is what I have now.

echo " ### 4.2.2 Ensure logging is configured (Not Scored) : ### "
echo " "
echo "Running command: "
echo "#sudo cat /etc/rsyslog.conf"
echo " "

sudo cat /etc/rsyslog.conf

Thank you!


Solution

  • Here is an example:

    MESSAGE=$(echo "Hello!")
    echo $MESSAGE
    Hello!
    

    Or the old standard:

    MESSAGE=`echo "Hello!"`
    echo $MESSAGE
    Hello!
    

    In your case:

    FILE_CONTENT=$(cat /etc/rsyslog.conf)
    

    NOTE!!!: It is very important to not use spaces near equal!

    This form is incorrect:

    MESSAGE = `echo "Hello!"`
    MESSAGE: command not found