LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 01-28-2012, 02:28 PM   #1
naushi
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Rep: Reputation: Disabled
using small bracket and its effect


When I runt he following program it give me a result of 10

y=10
(x=$y*3)
y=x
echo $y

then answer is 10

when I run the following

y=10
x=$y*3
y=x
echo $y

i get the answer 30

what is the explanation for the difference that the bracket is causing

thanks in advance
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-28-2012, 03:11 PM   #2
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by naushi View Post
When I runt he following program it give me a result of 10
what language is your code sample supposed to be?
It can't be C or C++, because the semicolons are missing, and the '$' is not a valid character in an identifier.
It can't be PHP, because the semicolons are missing, and y is something else than $y.
It can't be Javascript, because y is something else than $y.

So what is it?

[X] Doc CPU
 
Old 01-28-2012, 03:17 PM   #3
naushi
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
linux

language linux - redhat - bash command
 
Old 01-28-2012, 03:18 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Bash ?
echo $y should output 'x' in both cases, no ?
 
Old 01-28-2012, 04:03 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Cedrik is correct.

And for the record: those aren't "small brackets", they're commonly called
parentheses (specially in programming environments).
Code:
[] = brackets, square brackets
{} = braces, curly braces
() = parentheses, round brackets
<> = angle brackets


Cheers,
Tink
 
2 members found this post helpful.
Old 01-28-2012, 07:23 PM   #6
naushi
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
If I could find the answer I would not ask. I looked at differnt books and found out the command in parentheses are sub routines and they dont bring results itnot he main stream program. probably generally used to do some side calculation. Is that true? Then I wonder if the sub routine bring back value why cant the result be used in main stream program
 
Old 01-29-2012, 07:38 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Think of a subshell as calling a separate program that is using your value. Once the subshell / separate program has finished you are then returned to your script which
continues with the values it had prior to the previous call as it is not aware of any changes made.

A more precise explanation can be found here: http://mywiki.wooledge.org/SubShell
 
Old 01-29-2012, 07:57 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Neither of the commands you posted should work the way you describe them (edit: unless you've previously declared the variables to be integer only. But the next-to-last line still also needs "$x" instead of "x"). I'm going to add comments to explain exactly what's happening, then show you how it should work.

Code:
y=10		#set variable "y" to 10
(x=$y*3)	#set the variable "x" to the literal string "10*3".  But
		#the parens set it to operate in a subshell, which closes
		#after the command is run, and all changes are lost.
		#If you echoed "$x" after this, you'd get nothing. 
y=x		#set the variable "y" to the literal string "x"
echo $y		#echo the value of "y", which should give you "x"

###

y=10		#set variable "y" to 10
x=$y*3		#set the variable "x" to the literal string "10*3".
		#now you don't have a subshell, so the value is global.
		#If you echoed "$x" after this, you'd get "10*3".
y=x		#set the variable "y" to the literal string "x"
echo $y		#echo the value of "y", which should give you "x"
Your big problem above is that at no time are you operating in an arithmetic context, so all values are treated as strings. To run an arithmetic context, you should use either ((..)) or the let keyword.

Code:
y=10		#set variable "y" to 10
((x=y*3))	#inside an arithmetic context, set "x" to the value of y*3; meaning 
		#you get "x=30".  Notice that you don't need "$" in front of a
		#variable when in an arithmetic context, as anything other than a
		#digit or an operator is automatically assumed to be a variable.
y=$x		#set the variable "y" to the current value of "x" (which is 30).
echo "$y"	#echo the value of "y", which should now be "30".
See here for more on how to use arithmetic operators in bash:

http://mywiki.wooledge.org/ArithmeticExpression

Last edited by David the H.; 01-29-2012 at 08:09 AM. Reason: some corrections
 
2 members found this post helpful.
Old 01-29-2012, 08:09 AM   #9
naushi
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
thanks everyone for a great explanation
 
  


Reply



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
[SOLVED] grep Bracket Expressions Star_Gazer Linux - Newbie 2 04-10-2010 08:30 PM
Replace every last closing bracket ) by ); tucs_123 Linux - Newbie 2 09-18-2009 10:31 AM
PS1 bracket expressions? JimHughen Linux - Software 7 06-17-2009 01:45 PM
Tournament Bracket anamericanjoe General 1 09-19-2006 04:50 PM
Non-functioning bracket-key dekket Linux - General 1 04-21-2004 06:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08: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
Open Source Consulting | Domain Registration