I want to extract the fields in a bash script variable named $DequeuedItem. Fields are tab-delimited.
I tried with the following sentence:
DequeuedItemF1=$(echo $DequeuedItem | cut -f1)
But DequeuedItemF1 gets the whole $DequeuedItem, and it seems that tabs become regular spaces. Is the echo command doing a conversion of the tabs before the stream reaches the cut command?
When you
echoan unquoted variable, tabs are changed to spaces. Double-quotes will preserve the tabs:If the
DequeuedItemcontains no internal spaces, you could also usebut the first option is both clearer and more robust. (Note that there are two spaces after the backslash. The backslash escapes the first space, which becomes the delimiter for
cut, and the second space separates the-doption from the-foption. Confusing, and all the more reason to use the other choice!)