Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script to set environment variables

Tags:

shell

I wish to write a shell script to export variables.

Below I have listed the script .

echo "Perform Operation in su mode" export ARCH=arm echo "Export ARCH=arm Executed" export PATH='/home/linux/Practise/linux-devkit/bin/:$PATH'; echo "Export path done" export CROSS_COMPILE='/home/linux/Practise/linux-devkit/bin/arm-arago-linux-gnueabi-'; echo "Export CROSS_COMPILE done" 

But this doesn't seem to work properly. I have to individually execute the commands at the shell prompt instead.

like image 231
Prag Rao Avatar asked Aug 31 '13 11:08

Prag Rao


People also ask

How do I set an environment variable in shell?

You can set your own variables at the command line per session, or make them permanent by placing them into the ~/. bashrc file, ~/. profile , or whichever startup file you use for your default shell. On the command line, enter your environment variable and its value as you did earlier when changing the PATH variable.

How do I set environment variables in bash?

The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.

How do I pass an environment variable in bash script?

Environment Variables Bash scripts can also be passed with the arguments in the form of environment variables. This can be done in either of the following ways: Specifying the variable value before the script execution command. Exporting the variable and then executing the script.


1 Answers

You need to run the script as source or the shorthand .

source ./myscript.sh 

or

. ./myscript.sh 

This will run within the existing shell, ensuring any variables created or modified by the script will be available after the script completes.

Running the script just using the filename will execute the script in a separate subshell.

like image 155
mbaird Avatar answered Sep 21 '22 21:09

mbaird