Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pipe in command run using backtick?

Tags:

bash

pipe

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.

like image 957
Chakradar Raju Avatar asked Oct 26 '25 02:10

Chakradar Raju


1 Answers

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)
like image 188
anubhava Avatar answered Oct 27 '25 16:10

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!