I am using this guide as a reference.
I am able to run the command to find the length of a string, say
expr length 'monkey brains'
which returns 13 as expected
However I am having trouble with storing the result in a variable, say a variable called hi. First I tried straight up assigning hi
hi=expr length 'monkey brains'
which gave a command not found error. My thought process was then to wrap the command all in a string and then use $ to evaluate the string. So what I have is
hi="expr length 'monkey brains'"
echo $($hi)
but this didn't work either - expr: syntax error
Does anyone know what else I could try here or why my approach doesn't work?
I think this is what you're trying to do.
To store the output to a variable using command substitution:
You can also do this using parameter expansion in bash:
Both the Bash Hackers Wiki and the Bash Guide are good resources for information.
You may try this