Tuesday, February 17, 2015

Bash set variable to command output

In addition to the backticks, you can use $(), which I find easier to read, and allows for nesting.
 
OUTPUT="$(ls -1)"
echo "${OUTPUT}"

Quoting (") does matter to preserve multi-line values.

Src: http://stackoverflow.com/questions/4651437/how-to-set-a-bash-variable-equal-to-the-output-from-a-command

No comments:

Post a Comment