-1

I have a query say :

"select .... " > $my_result

This will store in the my_result but as a file. How to store in a my_result variable itself if that query gives only one row without creating a file.

supernatural
  • 1,107
  • 11
  • 34
  • Is this what you want? https://stackoverflow.com/questions/4651437/how-do-i-set-a-variable-to-the-output-of-a-command-in-bash – samthegolden Jul 17 '20 at 08:22
  • 1
    Does this answer your question? [How do I set a variable to the output of a command in Bash?](https://stackoverflow.com/questions/4651437/how-do-i-set-a-variable-to-the-output-of-a-command-in-bash) – samthegolden Jul 17 '20 at 08:22
  • But in this case, if the query gives some error, it will give be null or blank value. How to get 0 in this case – supernatural Jul 17 '20 at 08:34
  • [bashfaq How can I store the return value and/or output of a command in a variable?](https://mywiki.wooledge.org/BashFAQ/002) – KamilCuk Jul 17 '20 at 08:42

1 Answers1

0

Simplest way would be:

my_result=$(select ...)

And when you want to print it:

echo $my_result
alfheim
  • 107
  • 5
  • 1
    Please do not use backticks, it is [superseded and discouraged](https://wiki-dev.bash-hackers.org/scripting/obsolete). Use `$(...)` for command substitution instead. – KamilCuk Jul 17 '20 at 08:41
  • if there is say a connection issue, then it will return say blank, how to handle that case here `my_result=$(select ...)` `my_result1 should be 0 in this case – supernatural Jul 29 '20 at 15:23