Suppose I have a string with pipe separator:
str="1|2|3|4" I want them to be assigned to specific variables.
var_a=1 var_b=2 var_c=3 var_d=4 I am doing it in this way:
var_a="`echo $str | cut -d'|' -f1`" var_b="`echo $str | cut -d'|' -f2`" var_c="`echo $str | cut -d'|' -f3`" var_d="`echo $str | cut -d'|' -f4`" Can this be done in an efficient way? Please suggest.
echo $stris itself buggy;echo "$str"is slightly better (won't replace a*with a list of filenames, for example);printf '%s\n' "$str"is much better (works correctly with all possible values, including-nor-e, which some versions ofechowill consume). See BashPitfalls #14, and the APPLICATION USAGE section of the POSIXechospecification.