ardent
ardent

Reputation: 2503

How to echo an "*" character to console in shell

I'm trying to test a disk erase utility I've written with dd by looking at the following output:

00000000
*
00000200

I current wrote a short sample loop in shell script to iterate through the output and print it out just to verify the output:

dd if=/dev/zero of=sample bs=4M count=1
results=`dd if=sample bs=512 count=1 | hexdump -C | awk '{ print $1 } '`
for i in $results
do
   echo -e "$i"
done

but it prints out the directory listing when it hits the "*" character. If I try to escape it, then it'll just print out "$i". Using the following if-else construct didn't seem to help:

if [ "$i" == "\*" ] #using "*" didn't seem to work either
then
    echo -e "\*"
else
    echo -e "$i"
fi

Any ideas on where I am going wrong?

Upvotes: 2

Views: 923

Answers (4)

shellter
shellter

Reputation: 37258

Or, "thinking outside the box",

just use a different char? # ?

IHTH

Upvotes: 1

anubhava
anubhava

Reputation: 784998

No need of creating $results variable and then listing it in for loop.

You can list your output using awk itself:

dd if=sample bs=512 count=1 2>/dev/null | hexdump -C | awk '{ print $1 }'

Upvotes: 1

choroba
choroba

Reputation: 241818

The problem is that the asterisk is expanded in the for already. To avoid that, use read instead of the backticks and array instead of a string:

results=()
while read line ; do
    results+=("$line")
done < <( dd if=sample bs=512 count=1 | hexdump -C | awk '{ print $1 } ' )
for i in "${results[@]}" ; do
    echo -e "$i"
done

Upvotes: 2

epsilon
epsilon

Reputation: 2969

If you want to have exact content, quote with single quote

$ echo '*'

A bit of reading about quoting in bash here : http://tldp.org/LDP/abs/html/quoting.html

Upvotes: 1

Related Questions