How to store an output of shell script to a variable in Unix?
shell, unix
Solution
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.
Problem
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}`