When I run a grep
pipeline directly I get two lines of output:
$ grep Interface /proc/net/bonding/bond0 | cut -d : -f 2 | sed 's/ //g'
eno1np0
eno2np1
$
Strangely, the output is empty when I capture it and call echo
:
$ echo $(grep Interface /proc/net/bonding/bond0 | cut -d : -f 2 | sed 's/ //g')
$
A for
loop through grep's output shows many empty lines:
$ for a in $(grep Interface /proc/net/bonding/bond0 | cut -d : -f 2 | sed 's/ //g'); do echo $a; done
$
For what it's worth, I have tried redirecting stderr to stdout with no luck:
echo $(grep Interface /proc/net/bonding/bond0 2>&1 | cut -d : -f 2 | sed 's/ //g')
I even tried using cat
instead of grep
.
How can I get echo
to actually print what's being output by the grep
command? Normally, echo
works. I can echo output just I like normally would, for other things. I can echo text into a file. I just can't get it to work with this grep
pipeline.
CodePudding user response:
My hunch is that the results from /proc/net
actually contain null bytes, which throw off the shell.
What you are trying is quite similar to the infamous useless use of echo
though I suppose if the goal is to get all the output on a single line, it's not entirely useless (but still broken, because the shell will expand any wildcards in the results).
Perhaps try this:
grep Interface /proc/net/bonding/bond0 |
tr '\000' '\012' |
sed 's/^[^:]*://;s/ //g;N;s/^[^:]*://;s/ //g;s/\n/ /'
though the s/\n/ /
thing is probably not portable to all sed
variants.
CodePudding user response:
Figured this out, based on information from @CharlesDuffy and @tripleee, and a colleague of mine.
I am not sure it's a common occurrence, but posting here in case someone else runs into this in the future.
The cause was that the environment variable IFS
was set to a non-standard character. While cat
and grep
were working correctly - presumably because neither depends on IFS
to work - echo
was not.
I am not entirely sure how this happened, but for anyone that faces this issue, check your environment for a variable named IFS
(env | grep IFS
), and set it back to the default (' \t\n'
).
PS: bad start to a mid-week morning /facepalm