The value of col1
is the 13-character string ma${token}jid
. When you write echo ${col1}
with no quotes, the shell replaces ${col1}
by the value of the variable, then performs word splitting (i.e. breaking up the value into separate words at $IFS
characters) and filename generation (i.e. globbing). Variable values are not recursively expanded using shell syntax. If you use double quotes around variable substitution (which you should do unless you have a good reason not to), "$col1"
straightforwardly expands to the value of col1
.
If your strings are tame enough (i.e. contain no shell special characters, apart from the ${token}
that you want to substitute), then you can construct a string from $col1
that you evaluate as a shell snippet. In your example, the following line would print ma12345678jid
:
eval "echo $col1"
Alternatively, replace the marker ${token}
by the desired value. You can do it with a string substitution in bash:
while read -r col1 col2 col3; do
echo "${col1//'${token}'/$token}"
echo "${col2}"
echo "${col3}"
echo "********************************"
done <input.txt
You can also do it with awk
, as a pre- or post-processing step (depending on whether you want to perform the replacement in other columns as well).
<input.txt awk -v repl="$token" '{gsub(/\${token}/, repl); print}' |
while read …
You can perform the replacement with sed
, but only if you're sure that none of the characters &\/
and newline appear in the replacement text $token
(you can change the separator character from /
to something else if there's another character which you're sure doesn't appear in $token
).
<input.txt sed -e "s/\\\${token}/$token/" |
while read …