linux - [Solved-5 Solutions] How to set a variable to the output from a command in Bash ? - ubuntu - red hat - debian - linux server - linux pc
Linux - Problem :
How to set a variable to the output from a command in Bash ?
Linux - Solution 1:
In addition to the backticks, you can use $().
Quoting (") does matter to preserve multi-line values.
Linux - Solution 2:
You're using the wrong kind of apostrophe. You need `, not '. This character is called "backticks" (or "grave accent").
Linux - Solution 3:
The $(command) works as well, and it also easier to read, but note that it is valid only with bash or korn shells (and shells derived from those), so if your scripts have to be really portable on various Unix systems, you should prefer the old backticks notation.
Linux - Solution 4:
There are three ways to do:
1) Functions:
2) Another suitable solution could be eval:
3) Using variables directly:
You can get output of third solution in good way:
and also in nasty way: