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!
CodePudding user response:
variable=$(command)
variable=$(command [option…] argument1 arguments2 …)
variable=$(/path/to/command)
Taken from https://linuxhint.com/bash_command_output_variable/
CodePudding user response:
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