When I type echo Hello$'\n'world | cat -n I get the output as expected:
1 Hello
2 world
But if I want to number line of g++ -v | cat -n I get an unnumbered result.
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/10/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa:hsa
OFFLOAD_TARGET_DEFAULT=1
...
What’s wrong with my command?
>Solution :
The output of g++ -v goes to the standard error, not standard output. Redirect stderr to stdout to process it in a pipeline:
g++ -v 2>&1 | cat -n