I have a line of code
eval echo \$$var
which prints a string. How can I store this string into a variable?
To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...]
Here, arguments are parsed and combined into a string that will execute by the shell. `eval` returns an exit status code after executing the command. `eval` returns 0 as exit status code if no argument is provided or only null argument is provided.
On Unix-like operating systems, eval is a builtin command of the Bash shell. It concatenates its arguments into a single string, joining the arguments with spaces, then executes that string as a bash command.
Like this:
eval c=\$$var
a better, safer way is to use indirection:
c=${!var}
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