LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Variables and escape characters? (https://www.linuxquestions.org/questions/programming-9/variables-and-escape-characters-362123/)

flobadon 09-10-2005 02:03 PM

Variables and escape characters?
 
Hello all. I am supposed to write a simple script in bash for class, and have been having a heck of a time with something that should *probably* be pretty simple, here is what I need (or want, anyway)...

I am writing a script that does arithmetic via the commandline, for instance "./arithmetic 3 * 2" should output "3 * 2 = 6". So here is what I have written:

echo $1 $2 $3 = $(( $1$2$3 ))

Which works fine for everything except multiplication, since the asterisk gets interpreted as a wildcard. So, is it possible to make the script interpret it as the multiplication operator without entering an escape character into the command? Should I be using let instead?

Here are some rules for the writing of the script as given from my instructor:

"Your script should NOT use any conditional or repetition type statements.

Thanks in advance.

addy86 09-10-2005 02:22 PM

Maybe a completely different approach might be more helpful: have a look at bc.

The Advanced Bash-Scripting Guide is a very good source if you want to script in bash.

eddiebaby1023 09-10-2005 02:41 PM

Learn how to quote in the shell and it'll serve you for life. You can escape single characters with the \ (backslash) character and quote strings with single and double quotes. man sh (it's long!) describes it all, and it'll be well worth learning. Play around with
Code:

echo $HOME
echo \$HOME
echo "$HOME"
echo '$HOME'
echo hello world
echo hello                world
echo "hello              world"            again

and see how the behaviour differs.

Matir 09-10-2005 02:46 PM

Simply put, you should be able to quote each of the args in the substitiution operator. I would think $(( "$1$2$3" )) should work for you.

acid_kewpie 09-10-2005 04:34 PM

posting homework questions goes against the rules of using LQ.org. it is up to you to understand the work, not us.

Whilst all members responses here have been provided in an encouraging angle rather than a solution as such, members should remember not to provide stock answers in situations like these, as it doens't benefit anyone.


All times are GMT -5. The time now is 10:06 PM.