Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

unix

sh

I am new with unix and I am writing a shell script.

When I run this line on the command prompt, it prints the total count of the number of processes which matches:

ps -ef | awk '/siebsvc –s siebsrvr/ && !/awk/ { a++ } END { print a }'

example, the output of the above line is 2 in the command prompt.

I want to write a shell script in which the output of the above line (2) is assigned to a variable, which will be later be used for comparison in an if statement.

I am looking for something like

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

But when i run it, it says output could not be found whereas I am expecting 2. Please help.

like image 738
user3114665 Avatar asked Dec 19 '13 17:12

user3114665


People also ask

How do you assign a command output to a variable in UNIX?

To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...]

Which command should I use to display a variable?

The echo command is useful to display the variable's output especially when you know the content of a variable will not cause any issue.

How do you set a value to a variable in Linux?

A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables.


3 Answers

You can use a $ sign like:

OUTPUT=$(expression)
like image 162
Marutha Avatar answered Oct 12 '22 17:10

Marutha


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 $foo
UID PID PPID C STIME TTY TIME CMD

jed@MBP:~$ foo= $(ps -ef |head -1);echo $foo
-bash: UID: command not found
UID PID PPID C STIME TTY TIME CMD
like image 38
Jed Daniels Avatar answered Oct 12 '22 18:10

Jed Daniels


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

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

Or:

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

Example:

#!/bin/bash
output="$( bash <<EOF
echo first
echo second
echo third
EOF
)"
echo "$output"

Output:

first
second
third
like image 10
Jahid Avatar answered Oct 12 '22 18:10

Jahid