Assigning the output of a command to a variable [duplicate] Assigning the output of a command to a variable [duplicate] unix unix

Assigning the output of a command to a variable [duplicate]


You can use a $ sign like:

OUTPUT=$(expression)


Try:

output=$(ps -ef | awk '/siebsvc –s siebsrvr/ && !/awk/ { a++ } END { print a }'); echo $output

Wrapping your command in $( ) tells the shell to run that command, instead of attempting to set the command itself to the variable named "output". (Note that you could also use backticks `command`.)

I can highly recommend http://tldp.org/LDP/abs/html/commandsub.html to learn more about command substitution.

Also, as 1_CR correctly points out in a comment, the extra space between the equals sign and the assignment is causing it to fail. Here is a simple example on my machine of the behavior you are experiencing:

jed@MBP:~$ foo=$(ps -ef |head -1);echo $fooUID PID PPID C STIME TTY TIME CMDjed@MBP:~$ foo= $(ps -ef |head -1);echo $foo-bash: UID: command not foundUID PID PPID C STIME TTY TIME CMD


If you want to do it with multiline/multiple command/s then you can do this:

output=$( bash <<EOF#multiline/multiple command/sEOF)

Or:

output=$(#multiline/multiple command/s)

Example:

#!/bin/bashoutput="$( bash <<EOFecho firstecho secondecho thirdEOF)"echo "$output"

Output:

firstsecondthird