Saving the output of a command to a variable in Shell bash

Solution 1: If the output consists solely of simple words, you can write it like this.
Solution 2: It is a bit tricky due to the combination of quotes and double quotes where some need to be escaped. Nevertheless, the following command seems to work:
With your input, this provides me with an example.
For failure, you can perform the reverse check.
Solution: To capture the output of a command, like your pipeline, enclose it in either backticks or, preferably, in.

Solution 1:

To extract the field corresponding to the version number, you can pass the output of
ads2 version
and then through

$ version="$(ads2 version | grep 'ADS2 version' | cut -d' ' -f 3)"

Alternatively, utilize
for improved results.

$ version="$(ads2 version | awk '/ADS2 version/{print $3}')"

The version will then be saved in the shell variable named

$ echo $version

Regarding your edit’s follow-up question, you have the option to do something like this:

if [ "$(ads2 version | grep DDA | grep '(unknown)')" ]; then
    echo "Not activated"
    echo "Activated"

You restrict yourself to the line that includes the text
and verify if the text
is present on that line. If it is, then it is not activated; otherwise, it is.

Solution 2:

You have the option to utilize the os command and apply regex to extract the desired information from the output.

process = os.popen('ads2 version')
output =
res = re.findall(r"ADS2 version: (.*)", output.decode())

Providing the output of the ‘ads version’ would enable me to provide a more comprehensive response.

Frequently Asked Questions