Solved

Global variable in Unix shell programming

Posted on 2004-08-23
9
1,124 Views
Last Modified: 2013-12-26
How to declare a variable as global in unix shell programming so that it can be accessible by other shell pgms?

-Thanks
0
Comment
Question by:Thilak
9 Comments
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 50 total points
ID: 11869205
Hi Thilak,

> How to declare a variable as global in unix shell programming so that it can be accessible
> by other shell pgms?

There is no concept of declaring a variable in a script and you cannot access variables of one script from another ...
What you can do is output the values from the script and read them in another ... something like

var=`num1`

the backticks will cause execution of script num1 and resulting value will be stored in var ... you can access this value by using $var

echo $var

Sunnycoder
0
 
LVL 11

Expert Comment

by:avizit
ID: 11869258
or maybe you mean ENVIRONMENT VARIABLES

in that case check  http://www.ncl.ac.uk/ucs/unix/unixhelp/variables.html
0
 
LVL 11

Accepted Solution

by:
avizit earned 100 total points
ID: 11869267
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 11869281
Use environment variables with extreme caution ... moreover setting environment variables in a script will make it visible only to its children (sub shells) and not all other processes
0
 
LVL 11

Expert Comment

by:avizit
ID: 11869381
but it can be set in the

.cshrc file for csh and .bash_profile ( i think ) for bash  etc etc
0
 
LVL 2

Expert Comment

by:mansoor_a_khan
ID: 11876095
how abt setting it in .envfile is its for local user?
0
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 50 total points
ID: 11877004
1. Write the information in a file and have the other programs source it
i.e.  (assuming bourne shell)

cat << EOF > /path/to/filename
VAR1="$VAR1"
VAR2="$VAR2"
EOF

then
. /path/to/filename

within the other scripts to source filename into the current script

To make it visible to child processes, save the information in the environment, i.e.
export VAR1="blah"

then it's $VAR1
0
 
LVL 38

Assisted Solution

by:yuzh
yuzh earned 50 total points
ID: 11877032
I would put the ENV setting in files (one for sh/ksh/bash, another one for csh/tcsh)

eg:

for sh/ksh/bash

myenv.sh looks like:
LM_LICENSE_FILE=/opt/cds/share/license/license.807cf15c
CDS_LIC_TIMEOUT=300
export CDS_LIC_TIMEOUT LM_LICENSE_FILE
.....


for csh/tcsh
myenv.csh
setenv LM_LICENSE_FILE /opt/cds/share/license/license.807cf15c
setenv CDS_LIC_TIMEOUT 300
.....

To use them,

For sh/ksh/bash do:
. /path-to/myenv.sh

For csh/tcsh
source /path-to/myenv.csh

It will take care of the subshells!


0
 
LVL 6

Expert Comment

by:Talmash
ID: 11880095
hi ,

maybe u need setenv :

example :

setenv MY_VAR /project/leo102/gigemac/VERIF_TOOLS/Coverage/current/port

usage :

echo $MU_VAR

usage in perl :

$ENV{'MY_VAR'}

tal
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expand LInux Boot partition remotly 3 91
Perl Awk Need Help 3 119
Expand data scrubbing tool 13 32
Problem to event 3 98
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question