How to save the output of a command to a variable in Bash script? In Bash getting the output of a command line in terminal is sometimes necessary when doing Bash/Shell script. For example, if need to monitor a specific service whether it is installed, running or disabled. Getting the output of the command that checks the status of specific service is quite important, so the script will know on what to execute. If the specific output shows that the service has stopped then the script can decide to start the service, or if the output shows that a specific service or software is not installed then an option to install the software can be done. Here’s an example on how to save the status of a specific output to a variable using Bash. #!/bin/bash dcommand=$(systemctl status gdm.service) command_output=$(echo "$dcommand") str_running='Active: active (running)' if [[ "$command_output" == *"$str_running"* ]]; then echo "It's the...
Make the world a better place by sharing knowledge, ideas and anything that you can give that comes from the heart.