I'm looking for a the simplest one-instruction solution to manipulate a computed string.
For example, removing the last two characters on a computed string: md5sum returns with ' -' at the end.
I can do it in two instructions:
$ a=$(echo 'ab'|md5sum); a=$(echo ${a:0:-2})
$ echo $a # (not required instruction)
daa8075d6ac5ff8d0c6d4650adb4ef29
How do I do it with one instruction, like the following?
$ a=${(echo 'ab'|md5sum):0:-2}
-bash: ${(echo 'ab'|md5sum):0:-2}: bad substitution
Thank you.
echois a builtin). A real thing to do better isa="${a:0:-2}"instead ofa=$(echo ${a:0:-2}).read a _ < <(echo ... | md5sum)? But anyway, I agree with @KamilMaciorowski there's not much point to forcing this into one command.a=${"$(echo 'abcxx')":0:-2}, but other shells don't support nested expansions like that. (There's some oddity there where it requires the quotes to treat the command substitution output as a single string instead something split to a list. Of course you could also utilize that with the output ofmd5sum.)echo 'ab' | md5sumwill give you the hash ofab\n, not the hash ofab. You should probably be doingprintf 'ab' | md5suminstead so a\nisn't being added to themd5suminput, depending on how you're using the resulting hash.