You are not logged in.

#1 2012-10-25 13:50:20

Registered: 2012-01-18
Posts: 19

[SOLVED] bash, assign a "+" to a variable


I'm trying to write a simple calculator script in BASH (as a method of learning) and everything went fine until now. I'm able to go through arguments passed to my script, I can copy all the numbers, and check if they're really numbers (regexp). However I've got the problem with operators. When I try to copy operator sign + or - or * or / into variable

line 82: let: ELEMS[1]=+: syntax error: argument expected (error token is "+")

Here's the code which causes the error:

let ELEMS[$J]=${ARGS[$I]}

I and J are counters used inside my loop. The above instruction works fine for numbers, etc., but not for operators. What am I doing wrong?

I'm sorry if it's a stupid question but I can't find any answer neither on forum nor google.


Last edited by xartii (2012-10-25 14:36:40)


#2 2012-10-25 14:14:20

From: Ancapistan
Registered: 2010-07-12
Posts: 2,806

Re: [SOLVED] bash, assign a "+" to a variable

If you just leave out the 'let' it should work.  Let "carries out arithmetic operations on variables" and "cannot be used for setting string variables".  Those quotes are directly from  There's a lot of info there, so search the page for "The let command" to jump right to the appropriate section.

Also, you could have figured this out on your own if you had tried.  Googling 'bash let' returned the page I linked as the first result.

Last edited by alphaniner (2012-10-25 14:19:27)

But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner


#3 2012-10-25 14:21:07

Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 7,982

Re: [SOLVED] bash, assign a "+" to a variable

"let" evaluates arithmetic expressions, so things like "let x=+" don't make sense.

If you just want to have "+" in a variable, do so without using let. e.g. "x=+". You can then use that in a let statement:

$ x=+
$ echo $x
$ let answer="1${x}2"
$ echo $answer                                                                   

Mobo: MSI X299 TOMAHAWK ARCTIC // Processor: Intel Core i7-7820X 3.6GHz // GFX: nVidia GeForce GTX 970 // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 5x 1TB HDD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.


#4 2012-10-25 14:36:26

Registered: 2012-01-18
Posts: 19

Re: [SOLVED] bash, assign a "+" to a variable


Sorry, I feel so stupid now. I must have missed this part somehow.

Thanks again,


Board footer

Powered by FluxBB