I have a bash command,
a=`xyz | head -n 1 | awk '{print $2}'`
which was used to get version number
I was using it a number of times, to avoid redundancy, I decided to store it as a string and execute it whenever I need, but now only the stdout of xyz is getting stored to the variable.
This is how I'm doing it,
cmd="xyz | head -n 1 | awk '{print \$2}'"
a=`$cmd`
What am I doing wrong? How to fix it? Also suggest if there is a simpler/better way of achieving it.
suggest if there is a simpler/better way of achieving it.
You command can be shortened to:
xyz | awk '{print $2; exit}'
And you can create a function for this rather than storing in a string:
mycmd() {
xyz | awk '{print $2; exit}'
}
And use it as:
a=$(mycmd)
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