Ömer
Ömer

Reputation: 59

How to make echo ignore "*" but not ignore "$"

I'm trying to learn basic scripting in Unix/Linux OS. Task I have right now is to make a basic calculator. Format is:

calc <number> <operand> <number>

#!/bin/bash -u
PATH=/bin:/usr/bin ; export PATH
umask 022

echo -n "$1$2$3="

case "$2" in
'+' )   echo `expr $1 "+" $3` ;;
'-' )   echo `expr $1 "-" $3` ;;
'*' )   echo `expr $1 "*" $3` ;;
'/' )   echo `expr $1 "/" $3` ;;
* )     echo 'unknow operation'  ;;        # the "default" if nothing else matches
esac

what happens is, when user type '*' it replaces it with glob pattern. I cant use any quotation marks to prevent it

" doesn't prevent it from happening

' displays screen this

$1$2$3=

is there a way to keep variables with $ while preventing * from messing up the application?

What I want is when user types "calc 2 * 3" computer to return following

2*3=6

Upvotes: 0

Views: 170

Answers (1)

Geno Chen
Geno Chen

Reputation: 5214

This glob expansion is done by bash, which is before your shell script. So your shell script has nothing to do with it.

To fix it, you may use

$ calc 2 \* 3
# or
$ calc 2 '*' 3
# or
$ set -f # equivalent to "set -o noglob"
$ calc 2 * 3

Upvotes: 2

Related Questions