I noticed these two different fields behaviors using cut
command:
bash:~$ var=`cat /proc/cpuinfo | grep 'model name' | uniq | cut -d ' ' -f 3,4,5,6,7,8 `
echo $var
outputs
Intel(R) Core(TM) i7-3632QM CPU @ 2.20GHz
and:
bash:~$ echo `cat /proc/cpuinfo | grep 'model name' | uniq` | cut -d ' ' -f 3,4,5,6,7,8
outputs
: Intel(R) Core(TM) i7-3632QM CPU @
fields numbers are the same but different outputs. Why?
Best Answer
It is because the unquoted `` backquote command substitution has removed an extra space between the
model name
and the:
characters. Refer to the outputs without thegrep
to see the difference for yourselfand with
As a result the
cut
sees different fields from number3
onwards in both the cases. This can be fixed if you avoid using backticks and use$(..)
with a proper quoted substitutionBut that said, using
cat
/grep
etc sequentially can be avoided and a singleawk
can be used in-place of itOr even more exact, if a single leading space in the above result is worrisome, remove it using
sub
Or if you have
GNU
variant ofgrep
which has PCRE regex enabled, which you can use as