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}
Shell variables are stored in the memory of the running shell. Use any data structure that lets you easily look up an item given its name; a hash table is a good choice. The difference between shell variables and environment variables is that environment variables are placed in the environment of subprocesses.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With