ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I assume you're talking about shell scripts (in the future, it would probably be a good idea to specify the language and cut/paste some example code ... just to give everybody a clearer idea of what you're asking for).
Yes, variables are indeed global in shell scripts (*unlike* most other languages, where variables are usually local by default).
Here's an example:
Code:
:
myfunc () {
A=letterA
echo '>>Hello from myfunc!'
echo ">>A is $A..."
}
echo 'Hello from myScript...'
echo A is $A...
myfunc
echo A is $A...
Quote:
Hello from myScript...
A is ...
>>Hello from myfunc!
>>A is letterA...
A is letterA...
before testfn v1=variable1global
before testfn v2=variable2global
in testfn v1=variable1local
in testfn v2=variable2local
after testfn v1=variable1local
after testfn v2=variable2global
Thanks for the help. I thought this forum for only unix based questions, so asked my question directly. Next time I will remember to specify the programming language.
I have a follow on question on scripting.. is there a way to force the variable to be local in a function.
Actually, at the top of the forum it says
"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."
but don't worry about it, you know now.
I think matthew42 has already answered your follow-up qn.
I think typeset is compatible with ksh which is probably where I picked it up (I don't think local is in ksh). Good to know there's a more readable keyword in bash. Thanks unspawn
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.