How to Store a Command in a Variable in a Shell Script

How can I store a command in a variable in a shell script?

Use eval:

x="ls | wc"
eval "$x"
y=$(eval "$x")
echo "$y"

how do I store the output of a cut -c command into a variable in shell script

Try

my_var="$( echo  $filename | cut -c 15-20 )"

Demo:

$filename=Incoming_file_180420053826.csv 
$my_var="$( echo $filename | cut -c 15-20 )"
$echo $my_var
180420
$

Storage commands output as a variable in bash

You appear to be confusing storing the output of commands with storing the text of the commands themselves (which is almost always a bad idea). I'm not sure exactly what you're trying to do (or what the output of nas_server -list all looks like), but I suspect you want something like this:

nameserver="$(nas_server -list all |  awk '{print $6}')"   # $() captures the output of a command
echo "$nameserver" # Double-quote all variable references to avoid parsing weirdness!
nameserverreal="$(echo "$nameserver" |awk '/encap|nameserver_/{ print }')"
echo "$nameserverreal"

Here's a simplified version:

nameserverreal="$(nas_server -list all |  awk '$6 ~ /encap|nameserver_/ {print $6}'"

Oh, and anytime you're tempted to use eval in a shell script, it's a sign that something has gone horribly wrong.

Storing part of a command string as a variable in bash

You need to store all commands as strings, omitting '$(...)', with runs the command ... and returns its STDOUT (not what you want).

Then use echo "$cmd1 $cmd2 ..." | bash to execute the resulting command in the shell. The double quotes around all commands are important, to prevent treating some of the strings as the options to echo:

cmd1='command 1 string'
cmd2='command 2 string'

# Execute: command 1 string command 2 string
echo "$cmd1 $cmd2" | bash

Example 1:

cmd1='sudo'
cmd2='purge'

# Runs: sudo purge
echo "$cmd1 $cmd2" | bash

Example 2:

Script:

#!/usr/bin/env bash

tmpcmd="( echo 'foo' ; echo 'bar' ) | grep"
echo "with variable interpolation:"
echo "$tmpcmd 'foo'" | bash
echo "without variable interpolation:"
echo "( echo 'foo' ; echo 'bar' ) | grep 'foo'" | bash
echo "original command:"
( echo 'foo' ; echo 'bar' ) | grep 'foo'
echo 'done'

Output is identical regardless of whether the command was or was not interpolated and whether or not it was executed as is:

with variable interpolation:
foo
without variable interpolation:
foo
original command:
foo
done

Example 4 (based on the example by the OP, confirmed that it does not work as some expect):

#!/usr/bin/env bash
ls > /dev/null
tmpcmd="history | grep"
echo "with variable interpolation:"
echo "$tmpcmd ls" | bash
echo "without variable interpolation:"
echo "history | grep ls" | bash
echo "original command:"
history | grep ls
echo 'done'

Output:

with variable interpolation:
without variable interpolation:
original command:
done

Example 4 does not print any output from the history commands because bash disables history in non-interactive shells by default. It is best not to use history in such shell script examples. See also:

https://unix.stackexchange.com/q/5684/13411

https://unix.stackexchange.com/q/112354/13411

How do I set a variable to the output of a command in Bash?

In addition to backticks `command`, command substitution can be done with $(command) or "$(command)", which I find easier to read, and allows for nesting.

OUTPUT=$(ls -1)
echo "${OUTPUT}"

MULTILINE=$(ls \
-1)
echo "${MULTILINE}"

Quoting (") does matter to preserve multi-line variable values; it is optional on the right-hand side of an assignment, as word splitting is not performed, so OUTPUT=$(ls -1) would work fine.

save an output of a command in a variable in Bash scripting

You could pipe the output of ads2 version to grep and then to cut to extract the field corresponding to the version number:

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

or better using awk instead:

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

The version will be then stored in the version shell variable:

$ echo $version
4.8.10

As for the follow-up question of your edit, you could do something like:

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

That is, you limit yourself to the line containing the text DDA and then check whether you have the text (unknown) on that line. If so, it's not activated; otherwise, it is.

How do i store the output of a bash command in a variable?

PROCESS=$(echo "$LINE" | awk '{print $2}')

or

PROCESS=$(ps aux | grep "$1" | awk '{print $2}')

I don't know why you're getting the error you quoted. I can't reproduce it. When you say this:

PROCESS=$LINE | awk '{print $2}'

the shell expands it to something like this:

PROCESS='mayoff  10732 ...' | awk '{print $2}'

(I've shortened the value of $LINE to make the example readable.)

The first subcommand of the pipeline sets variable PROCESS; this variable-setting command has no output so awk reads EOF immediately and prints nothing. And since each subcommand of the pipeline runs in a subshell, the setting of PROCESS takes place only in a subshell, not in the parent shell running the script, so PROCESS is still not set for later commands in your script.

(Note that some versions of bash can run the last subcommand of the pipeline in the current shell instead of in a subshell, but that doesn't affect this example.)

Instead of setting PROCESS in a subshell and feeding nothing to awk on standard input, you want to feed the value of LINE to awk and store the result in PROCESS in the current shell. So you need to run a command that writes the value of LINE to its standard output, and connects that standard output to the standard input of awk. The echo command can do this (or the printf command, as chepner pointed out in his answer).



Related Topics



Leave a reply



Submit