Bash – Cut works with echo but not without it

bashcutechofileslinux

So I am trying to extract an output similar to

x=($discover nginx --human=nood)

which gives me an output like

i-03099 nginx IP noodlefish pip b4b966d280546c6b070f5f952c281d3294308048

Further I want to extract the pip column in another variable.
When I do

echo "$x" | cut -f6

I get my desired output, but when I try.

y= "$x" | cut -f6 

I get a blank output.

Please can you explain me why is this happening and how can I get the result I want. Thank you in advance.

Best Answer

Further I want to extract the pip column in another variable. When I do

echo "$x" | cut -f6

I get my desired output[...]

That's weird, because this should not work, since

‘-f FIELD-LIST’
‘--fields=FIELD-LIST’
     Select for printing only the fields listed in FIELD-LIST.  Fields
     are separated by a TAB character by default.  Also print any line
     that contains no delimiter character, unless the ‘--only-delimited’
     (‘-s’) option is specified.

-f should work only when fields are separated by TAB, unless -d sets it otherwise.

If you want to extract the field after the word pip, such script works for me

x="i-03099 nginx IP noodlefish pip b4b966d280546c6b070f5f952c281d3294308048"

y=$(echo -n "$x" | cut -d ' ' -f 6 -)

echo "$y"
Related Question