Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm making a bash script to do basic arthmetic and when I do:

if [ $2 == "*" ]

it does not work.

How can I check for asterisk?

share|improve this question
Unix Philosophy 102: Let the shell do what the shell does best, and use bc or dc if you want a calculator. – JdeBP Nov 25 '11 at 9:56
up vote 3 down vote accepted

You need to escape or quote the asterisk in the command line:

./ 2 \* 2
./ 2 '*' 2

and enclose the $2 in double quotes:

if [ "$2" == "*" ]
share|improve this answer
Oh good. For a moment I thought I'd need to beat someone over the head with the BASH FAQ. – Ignacio Vazquez-Abrams Nov 25 '11 at 1:55

The problem isn't that the if statement isn't working, it is that the asterisk on the command line is being globbed.

So if your script was called mycalc and run from the command line you do

mycalc 2 * 3

The * will get globbed, and converted to all the names of the files in the current folder.

To avoid expansion, you would need to do

mycalc 2 \* 3

The \ escapes the asterisk and passes it through without changing it.

You might want to consider x for the multiplication operation to avoid this.

share|improve this answer

Find ASCII value of * by using the command below

printf "%d\n" "'`echo "*" | awk '{print $1}'`"

and use that value for condition checking.

share|improve this answer
Unfortunately you do not say how to use the ASCII value for the condition checking. – pabouk Oct 10 '13 at 17:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.