SnailTang
SnailTang

Reputation: 23

read[command] in bash

When I read[command] some lines including character '*', it seems that '*' will be looked as a wildcard. whether exsits some solutions leting '*' just be a '*', please!

Upvotes: 1

Views: 192

Answers (3)

glenn jackman
glenn jackman

Reputation: 246799

It depends how you use the variable: if you quote it, filename expansion will not happen. Example:

$ ls
f1  f2  f3
$ read line
*
$ echo "$line"
*
$ echo $line
f1 f2 f3

Upvotes: 1

Steve Weet
Steve Weet

Reputation: 28392

If you do not want any of the special file name characters to be used as wildcards then enter the following in your script before the read.

set -o noglob

This will prevent the * ? and [] from having special meaning and treat them as normal characters.

The following example demonstrates the point

touch 1 2 3
echo "With wild card expansion"
echo *

echo "Without wild card expansion"
set -o noglob
echo *
And produces the following results

With wild card expansion
1 2 3 
Without wild card expansion
*

Upvotes: 0

alex
alex

Reputation: 490213

You can escape it with the escape character: \*. This means the * will be a literal *, not matching one or more characters as the glob pattern.

Upvotes: 0

Related Questions