Thread: Preventing Asterisk Expansion in Function Params in Bourne Shell Script

  1. #1
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856

    Preventing Asterisk Expansion in Function Params in Bourne Shell Script

    I'm writing a shell script in which I am performing floating point calculations. I cannot use `expr` since it does not support real numbers, so I decided to write/use a function to use `bc` like this:
    Code:
    math(){
      echo $1 | bc -l
    }
    
    result=`math "2.4 * 6.8"`
    The problem is that the asterisk is expanded to all filenames in the current directory when it is passed to math(). Escaping it (\*) does not help because then `bc` won't accept the string.

    Have you any ideas besides a hack in which I use escapes then remove them in math()? That may be the route I'll be forced to take, but I'd prefer something cleaner.

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Wow. Clearly I overlooked something quite elementary. Enclosing $1 in quotes prevents using the asterisk as a wildcard:
    Code:
    math(){
      echo "$1" | bc -l
    }
    Don't you hate when you try for hours to fix something then realize how slight your oversight was?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  2. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  3. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM
  5. how to set a function to an alias in C shell
    By xstone in forum Tech Board
    Replies: 1
    Last Post: 09-29-2001, 03:41 PM