How to define multiple environment variables in a shell script?

Hi All,
I am running a shell script on centos 7 to get some variable values and want to declare them as environment variables so that i will use them in another shell script.

I am using below script i am able to define one environment variable but it's not allowing me to do for another variable.

Can someone please help me on this issue.

 path=$(find $config_path -name abc.txt 2>/dev/null )

domain_path=$(cat $path |grep "domain location" )
 
boot=$(find $config_path -name boot.properties 2>/dev/null |head -1 )

 user_name=$(cat $boot |grep username)
echo 'export NAME=$user_name' >> ~/.profile

  passwd=$(cat $boot |grep password)
  echo $passwd
  echo 'export TEST=$passwd' >> ~/.bash_profile

What exactly is the issue? As always: pls describe the error meticulously, add error messages and misbehaviour.

What I can say from looking at your script is that shell variables are not expanded within single quotes, so neither $user_name nor $passwd values will be passed to the .*profile files. If that is by intention, make sure the vaiables are defined in those scripts.

You alter both ~/.profile and ~/.bash_profile.

~/.bash_profile is for a bash login shell.
At login ~/.profile is for all sh-style shells (sh,ash,dash,jsh,psh,ksh,bash,...), but bash runs ~/.bash_profile instead if present.
Some graphical desktops provide a login but do not run a login shell, then bash runs ~/.bashrc.
Some Linux distros might run ~/.bash_profile (or ~/.profile) from /etc/bash.bashrc that is run prior to ~/.bashrc.

Yes, it's a mess.

Hi Rudi,

I am trying to set multiple environment variables using shell script.

I tried below commands to set the environment variables in the shell script(as example i shared one variable info):

abc=$(find $config_path -name xyz.xml 2>/dev/null)
user_name=$(cat $abc |grep username )
export NAME=$user_name

when i executed the shell script it's executed successfully but when i am trying to print the environment variable it's returning empty value
echo $NAME -- is giving empty value.

Can you please let me know what i need to change to set the environment variable ?.

EXPORT allows shell variables to be passed onto sub processes of the current shell not back up to the calling process.

If you want a child script to set environment variables in the parent (calling) script source the child script. Example:

$ NAME=
$ cat sc_1
abc="Test abc"
def=${abc% *}
export NAME=$def
$ ./sc_1
$ echo $NAME

$ . ./sc_1
$ echo $NAME
Test

See how the second instance sc_1 is sourced into the current shell by using . ./sc_1 (or source ./sc_1 if you prefer the clearer long-hand form). Any variables set will now be in the current shell. Think of this as reading the commands in the child script into the current process not spawning a new shell to run sc_1 which loses any environment vars after it finishes.

2 Likes

$NAME being empty is a strong indicator that $user_name is empty and the "command substitution" fails / results in an empty string. I guess its because the file called $abc doesn't exist. and the cat cats nothing.

It would help if people knew the entire scenario. Variables in which environment do you want to set? Your current session? Future sessions? Why the echo ing into the varying .profiles?

You're using find to search directories, nice.
But then you're using the variable without checking if it's has one , none or more results.

Further, I assume that you want the variable $user_name to actualy contain (just) the "<username>", and not the string "username + <username>".
This said, you might want to take care of that, afterwhich you might want to use something like this:

if [[ -n "$user_name" ]]
then    export NAME=$user_name
else    echo "<user_name> is empty"
        # If it is within a script:
        #exit 1
fi

As the string might become empty after removing the found string pattern from it's content ("username + <username>"), if it even finds anything - as in your current case.
This said, you might want to make sure where your find starts from.

I would also suggest to take care of multiple finds (tempfiles) within a a for loop.
This is my suggested handling for all dynamic (find, locate, *, ls, which, etc) variables.

Hope this helps