Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.