LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-11-2020, 12:55 AM   #1
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Rep: Reputation: 19
bash arithmetic evaluation null variable strange behavior


a is unset. Why would this happen?
Code:
$ echo $(($a+1))
1
$ echo $((1+$a))
bash: 1+: syntax error: operand expected (error token is "+")
$ bash --version 
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
 
Old 01-11-2020, 12:58 AM   #2
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Original Poster
Rep: Reputation: 19
+ in +1 works like a unary operator, + in 1+ is wrong.
 
Old 01-11-2020, 06:38 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Just as an FYI from the manpage (emphasis by me):
Code:
ARITHMETIC EXPRESSIONS
       ...

       Shell variables are allowed as operands; parameter  expansion  is  per‐
       formed before the expression is evaluated.  Within an expression, shell
       variables may also be referenced by name without  using  the  parameter
       expansion  syntax.  A shell variable that is null or unset evaluates to
       0 when referenced by name without using the parameter expansion syntax.
       The  value  of a variable is evaluated as an arithmetic expression when
       it is referenced, or when a variable which has been given  the  integer
       attribute using declare -i is assigned a value.  A null value evaluates
       to 0.  A shell variable need not have its integer attribute  turned  on
       to be used in an expression.
So the expression
Code:
$ unset a
$ echo $((1+a))
1
is allowed and does not lead to an error.
 
2 members found this post helpful.
Old 01-11-2020, 07:44 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
To expand on what has already been said...

Code:
cat null_math
#!/bin/bash
unset a
echo $((a+1))
echo $((1+a))
echo $(($a+1))
echo $((1+$a))

./null_math
1
1
1
./null_math: line 8: 1+: syntax error: operand expected (error token is "+")
You will see that only the last case fails.

The first one evaluates to 0+1 per the pasted man page snippet.

The second one evaluates to 1+0, again per the man page.

The third, which uses parameter expansion, evaluates to +1 which is just the value, +1.

The fourth, also using parameter expansion, fails because it evaluates to 1+ (nothing after the +), hence the message about the missing operand.
 
1 members found this post helpful.
Old 01-13-2020, 02:08 AM   #5
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Original Poster
Rep: Reputation: 19
Quote:
A null value evaluates to 0.
Apparently this refers to:
Code:
 ~/tmp]$ unset a
 ~/tmp]$ echo $(()) $((a)) $(($a))
0 0 0
They tell the story of the program, so to speak, in a concentrated way, and, of course, it can not be perfectly clear.

(@astrogeek: I fully disagree with your signature. Sources are information, just like software. Trade in information is subject to lemon dilemma and moral hazard.)

Last edited by doru; 01-13-2020 at 02:26 AM.
 
  


Reply

Tags
bash, variable



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange bash completion behavior when using path with ' (apostroph) in PATH variable mallniya Linux - Newbie 3 02-05-2014 12:41 AM
[SOLVED] changing variable values with unary vs arithmetic operators atlantis43 Linux - Newbie 9 05-15-2013 09:05 PM
[SOLVED] Strange behaviour in bash comparison when variable contains arithmetic operator michael.wegemer Programming 3 10-19-2010 05:22 AM
-bash: HISTSIZE: readonly variable -bash: HISTFILESIZE: readonly variable deathsfriend99 Linux - Newbie 4 12-08-2009 12:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration