Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you export a variable through shell script?

Tags:

a.sh

#! /bin/sh export x=/usr/local 

we can do source ./a in command-line. But I need to do the export through shell script.

b.sh

#! /bin/sh . ~/a.sh 

no error... but $x in command-line will show nothing. So it didn't get export.

Any idea how to make it work?


a.sh

#! /bin/sh export x=/usr/local ----------- admin@client: ./a.sh admin@client: echo $x  admin@client:  <insert ....> 
like image 912
CppLearner Avatar asked Jun 18 '12 02:06

CppLearner


People also ask

How do I export a variable in shell script?

You can use the export command to make local variables global. To make your local shell variables global automatically, export them in your . profile file. Note: Variables can be exported down to child shells but not exported up to parent shells.

How do I export a variable in Linux?

To export a environment variable you run the export command while setting the variable. We can view a complete list of exported environment variables by running the export command without any arguments. To view all exported variables in the current shell you use the -p flag with export.

How does the variable export in bash?

To export the variable in bash, open Terminal from Menu on the bottom left on the screen in your computer system. Click on the Terminal option. Once the terminal is opened, you need to provide a variable, let's call it vech for the sake of ease. We will then assign it a value for now, i.e., “Bus”.


1 Answers

You can put export statements in a shell script and then use the 'source' command to execute it in the current process:

source a.sh 

I hope this helps.

like image 187
Emmanuel Avatar answered Oct 05 '22 10:10

Emmanuel