How to escape quotes and pipe?
#!/bin/bash
set -x
MYCMD="VBoxManage showvminfo --machinereadable $1 \| grep \'VMState=\"poweroff\"\'"
echo "`$MYCMD`"
Executed command :
++ VBoxManage showvminfo --machinereadable d667 '|' grep '\'\''VMState="poweroff"\'\'''
And finally getting this error:
Syntax error: Invalid parameter '|'
You don't; you would need to use eval
to embed an arbitrary pipeline in a regular string parameter.
MYCMD="VBoxManage showvminfo --machinereadable \"$1\" | grep 'VMState=\"poweroff\"'"
eval "$MYCMD"
However, this is not recommended unless you are certain that the value of $1
will not cause problems. (If you need an explanation of what those risks might be, then you should not be using eval
.)
Instead, define a shell function:
mycmd () {
VBoxManage showvminfo --machinereadable "$1" | grep 'VMState="poweroff"'
}
mycmd "$1"
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