Explaination on export command

Hello Team,

Could you pls explain how export command works in below code:

for i in ${!SDV_*}; do
        export $i
    done

As per my understanding, if

SDV_1=test1;SDV_2=test2;test1=var1;test2=var2

then in for loop below export will get executed.

export var1;export var2

But, Will this actually work like normal export command below?

export SDV_1=test1;
export test1=var1

Could you explain and correct me if wrong.

also i tried below and did not get the values in the new sessions.

On Same session:

$ var1=test1;
$ export var1;
$ echo $var1
test1
$ export var2;
$ var2=test2;
$ echo $var2
test2
$

On new session:

$ echo $var1

$ echo $var2

$

Regards,
Chandana

Hello chandana,

Welcome to forums. If you want to add a variable's value permanently then you can add it in your .profile and then it will work for new sessions also.
Let me give you an example here.

Let's say we want to set prompt sign for a specific user then we can add it in .profile as follows.

export PS1="${LOGNAME}@$(hostname)$ "

Also following link may help you in same to understand it better.
https://in.answers.yahoo.com/question/index?qid=20091118140017AAo0wb2

Thanks,
R. Singh

1 Like

Thanks Ravinder. :slight_smile:

Could you/Team explain on how the for loop is working in below code block?

for i in ${!SDV_*}; do         export $i     done

Regards,
Chandana

---------- Post updated at 02:59 PM ---------- Previous update was at 01:10 PM ----------

Thanks.

Yes, it seems i understand now.

Export helps to define a variable for the current and in its child sessions.

But we have to define/add it to bash_rc file for permanent for all the sessions(child or totally a fresh session).

Correct me if wrong.

Regards,
Chandana

export $i

will export the i variable, which you then overwrite several times in your loop.
Children do not necessarily run bashrc when created, so you may need to take extra action to have those variables available.