1
_test=`shasum -a 256 my "file here.txt" | awk -F' ' '{print $1}'`

This works as expected. But the OCD in me wants to use something like

_test="${my-command-here}"

But because of the single quotes in the awk section, I'm getting a bad substitution error.

$ _test="${shasum -a 256 "my file here.txt" | awk -F' ' '{print $1}'}"
zsh: bad substitution
$ _test=${shasum -a 256 \"my file here.txt\" | awk -F\' \' \'{print $1}\'}
zsh: bad substitution

Any ideas on how to get it to work? Thanks

1

What you are doing with ${...} is called Parameter Expansion. This will expand parameters - in your case, a parameter called shasum -a 256 "my file here.txt" | awk -F' ' '{print $1}', but that's not a valid parameter name, as described in Parameters:

A name may be any sequence of alphanumeric characters and underscores, or the single characters ‘*’, ‘@’, ‘#’, ‘?’, ‘-’, ‘$’, or ‘!’

What you actually want is to run everything in ${...} and for that you need Command Substitution, which uses parentheses instead of curly braces:

↪ touch "my file here.txt"
↪ _test="$(shasum -a 256 "my file here.txt" | awk -F' ' '{print $1}')"
↪ echo $_test
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.