1

I have a text file which contains an asterisk as the first character in every line, when I try to process the file in a script like this:

while read line; do
  out=$(echo $line | awk '{ print $3 }')
  echo $out
done < file_with_asterisks

It is printing the files in the current working directory, not the 3rd column as I want.

Removing the asterisk solves the problem, so I figured out it was doing globbing.

How do I make bash take the asterisk as text and not a wildcard?

Anthon
  • 79,293
loki
  • 215

1 Answers1

2

You should put quotes around your variables:

while IFS= read -r line; do
  out=$(echo "$line" | awk '{ print $3 }')
  echo "$out"
done < file_with_asterisks

In your case the echo $line expands (I am assuming a space follows the initial asterisks on each line)

cuonglm
  • 153,898
Anthon
  • 79,293