简体   繁体   中英

Bash: echo showing blank value

I have two variables as follows

SampleOutput=`some command giving output`
Status=`echo "$SampleOutput" | grep -qs "Active"`
echo $SampleOutput
echo $Status

Here $SampleOutput has value as AgentEnable=Active bla bla bla

However, $Status is coming as blank I am not sure why $Status is coming as blank when it should have value AgentEnable=Active

When using grep -q you don't get any output from grep . Only return status is available that you can get using:

grep -qs "Enable" <<< "$SampleOutput"
Status=$?

As per man grep :

-q, --quiet, --silent Quiet mode: suppress normal output. grep will only search a file until a match has been found, making searches potentially less expensive.

Note that if you are not using SampleOutput anywhere else then you can directly use:

some command | grep -qs "Enable"
Status=$?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM