- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- defining a global variable
Operating System - HP-UX
1758417
Members
3037
Online
108869
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2005 02:52 AM
03-22-2005 02:52 AM
Re: defining a global variable
In general all shell variable are global within a shell script. However: If you are calling non shell routines within the script then the varables within those routines will be local to that routine. What I meen by a non shell routine is if you are using awk, perl, sql, or other executables that can be called in-line from shell that is not shell. Usually you will have to pass varables to this routines. I.E. awk -v INDATE=${INDATE}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2005 03:17 AM
03-22-2005 03:17 AM
Re: defining a global variable
Actually, one can have local variables within a shell script. Those defined in functions via the typeset command are local to the function. Run this script and it should make variable scoping clear.
--------------------------------------------
#!/usr/bin/sh
typeset -i I=100 # global
typeset GLOVAR="Global String"
func1()
{
typeset -i I=1 # local definition
echo "I in func1 = ${I}"
echo "GLOVAR in func1 = ${GLOVAR}"
return 0
} # func1
func2()
{
typeset -i I=2 # local definition
echo "I in func2 = ${I}"
echo "GLOVAR in func2 = ${GLOVAR}"
return 0
} # func2
func1
func2
echo "I in main = ${I}"
echo "GLOVAR in main = ${GLOVAR}"
exit 0
----------------------------------------
It's considered good practice to typeset all variables in scripts. Of course, any of these can be exported and made available to child processes.
--------------------------------------------
#!/usr/bin/sh
typeset -i I=100 # global
typeset GLOVAR="Global String"
func1()
{
typeset -i I=1 # local definition
echo "I in func1 = ${I}"
echo "GLOVAR in func1 = ${GLOVAR}"
return 0
} # func1
func2()
{
typeset -i I=2 # local definition
echo "I in func2 = ${I}"
echo "GLOVAR in func2 = ${GLOVAR}"
return 0
} # func2
func1
func2
echo "I in main = ${I}"
echo "GLOVAR in main = ${GLOVAR}"
exit 0
----------------------------------------
It's considered good practice to typeset all variables in scripts. Of course, any of these can be exported and made available to child processes.
If it ain't broke, I can fix that.
- « Previous
-
- 1
- 2
- Next »
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP