47

i have a shell script "script.sh" which gives output as "success" or "Failed" when i execute in unix window. Now i want to store the output of script.sh into a unix command variable. say $a = {output of script.sh}

logan
  • 7,946
  • 36
  • 114
  • 185
  • possible duplicate of [shell script can not save output from command line into variable](http://stackoverflow.com/questions/10487379/shell-script-can-not-save-output-from-command-line-into-variable) – Charles Duffy Jun 08 '12 at 12:25
  • Pretty much the same as [redirect command output into variable and standard output in ksh](http://stackoverflow.com/questions/7901568/redirect-command-output-into-variable-and-standard-output-in-ksh) – Dan Dascalescu Apr 03 '14 at 07:48

5 Answers5

96

Two simple examples to capture output the pwd command:

$ b=$(pwd)
$ echo $b
/home/user1

or

$ a=`pwd`
$ echo $a
/home/user1

The first way is preferred. Note that there can't be any spaces after the = for this to work.

Example using a short script:

#!/bin/bash

echo "hi there"

then:

$ ./so.sh
hi there
$ a=$(so.sh)
$ echo $a
hi there

In general a more flexible approach would be to return an exit value from the command and use it for further processing, though sometimes we just may want to capture the simple output from a command.

Levon
  • 138,105
  • 33
  • 200
  • 191
7

Suppose you want to store the result of an echo command

echo hello   
x=$(echo hello)  
echo "$x",world!  

output:

hello  
hello,world!
jean
  • 4,159
  • 4
  • 31
  • 52
user3122919
  • 71
  • 1
  • 2
4

You should probably re-write the script to return a value rather than output it. Instead of:

a=$( script.sh ) # Now a is a string, either "success" or "Failed"
case "$a" in
   success) echo script succeeded;;
   Failed) echo script failed;;
esac

you would be able to do:

if script.sh > /dev/null; then
    echo script succeeded
else
    echo script failed
fi

It is much simpler for other programs to work with you script if they do not have to parse the output. This is a simple change to make. Just exit 0 instead of printing success, and exit 1 instead of printing Failed. Of course, you can also print those values as well as exiting with a reasonable return value, so that wrapper scripts have flexibility in how they work with the script.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • As a side note, you'll want to use `success` and `failed` (note the consistent capitalization) in any real code, or things will get confusing quickly. – Nic Jan 14 '16 at 17:38
3
export a=$(script.sh)

Hope this helps. Note there are no spaces between variable and =. To echo the output

echo $a
gandalf007
  • 87
  • 7
  • when the script will execute ? after submitting echo $a? or after export ? – logan Jun 08 '12 at 11:56
  • Why are you exporting `a`? You should use double quotes around `$a` in the `echo` statement. – Sven Marnach Jun 08 '12 at 11:56
  • @login it will run the script in a subshell and wait for it until it quits, that subshell's streams will be pointing to the variable "a", meaning that everything they output will be written to the variable instead of the standard output of your main shell – Jeffrey Vandenborne Jun 08 '12 at 11:59
  • 1
    @SvenMarnach I didn't know how the OP wanted to use the variable, exporting makes it available to child processes – gandalf007 Jun 08 '12 at 12:04
  • 2
    @logan $(command) is a way to run commands and capture output – gandalf007 Jun 08 '12 at 12:05
0

You need to start the script with a preceding dot, this will put the exported variables in the current environment.

#!/bin/bash
...
export output="SUCCESS"

Then execute it like so

chmod +x /tmp/test.sh
. /tmp/test.sh

When you need the entire output and not just a single value, just put the output in a variable like the other answers indicate

  • No reason for "export" when using source. Also, for only a single value, exit status or stdout seem that they would make more sense. – Charles Duffy Jun 08 '12 at 12:27