LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-12-2011, 06:52 AM   #1
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 89

Rep: Reputation: 16
bash man not followed: ((?:)) requires paranthesis on third operand to accept vars


Code:
echo $((myvar<5?myvar=7:myvar=myvar-1))
does not work,
Code:
echo $((myvar<5?myvar=7:(myvar=myvar-1)))
works. This is also documented here: http://www.scribd.com/doc/51950177/440/Logic (scroll down two pages).

Am I right, is this really a bug?

In fact, it requires parantheses on the third operand to accept var=.

Last edited by doru; 07-12-2011 at 07:01 AM. Reason: correction
 
Old 07-12-2011, 09:39 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
No it is not a bug. Did you read the error messages from your first code?
You are missing he point that this construct is being strict about what it is doing.
You can look at your first one like so:
Code:
echo $((myvar<5?myvar=7:myvar=myvar-1))
Everything in blue is part of the construct ?:, so once completed it then looks at the information in red which as it is an assignment it cannot
be assigned to an expression.

Your second version of course overcomes this by placing parenthesis around the assignment and hence it is performed prior to returning the value.

Strictly speaking you did not have to go through all this anyway:
Code:
echo $((myvar = myvar<5?7:--myvar))
 
1 members found this post helpful.
Old 08-15-2011, 08:17 AM   #3
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 89

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by grail View Post
No it is not a bug.
Thank you for your answer. It helps me understand what is going on.

Still I don't understand why is "=" not treated just like any other operator, for example like "<" or "--", since they do belong to the same list under "ARITHMETIC EVALUATION" in bash man. What makes ?: end before it meets "))"?

"?:" has higher precedence than "=".

Last edited by doru; 08-15-2011 at 08:22 AM. Reason: afterthought
 
Old 08-15-2011, 08:59 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Maybe this table can help.

Ultimately each part of the trinary operator must be complete, but you will see from the table that the '=' is lower in precedence
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash: list vars and function CarLost Programming 3 02-21-2009 02:02 PM
Case statements and vars for bash script geech Programming 1 01-09-2009 05:49 PM
Perl with bash environment vars rose_bud4201 Programming 6 06-21-2006 01:19 PM
Adobe Acrobate Reader Requires A Plugin-That Requires Open LDAP That Requires Berkely Old_Fogie Slackware 10 05-08-2006 06:04 AM
'bash'ing vars (dumping same) 3inone Programming 1 07-26-2003 11:06 AM


All times are GMT -5. The time now is 01:03 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration