Can please someone explain, why this is not working??
#!/bin/bash
cmd="ps aux | grep -v grep"
cnt=$($cmd)
I get an error from ps.
error: garbage option
Usage:
ps [options]
.....
"ps aux" only will be ok - but not with any additional piped commands.
Thanks!
It is better and safer to use function to store a pipeline command as:
unset cmd cnt
cmd() {
ps aux | grep -v grep
}
and use it in command substitution as:
cnt="$(cmd)"
See BASH FAQ on storing command line in a variable
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