7

This question already has an answer here:

I have created env.sh file the contents of which are below

#!/bin/sh

export M2_HOME=/vagrant/tools/maven
export PATH=$PATH:$M2_HOME/bin
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_65
export ANT_HOME=/usr/local/apache-ant-1.9.7
export MAVEN_HOME=$M2_HOME

but executing this sh file does not result in env variables being exported. Have to manually execute each line to get them exported. Please help

marked as duplicate by Scott, Wildcard, Anthon, roaima, GAD3R Sep 27 '16 at 8:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

12

You need to source (.) the file, not execute:

. env.sh

When you execute the script, it is run in a subshell, and all the parameters exported will go out of scope once the (sub)shell exits, not affecting the parent shell.

When you source it, the parameters will be available on the calling session. And also on the child processes as you are export-ing them.


Note that, the default system shell (sh) for Ubuntu is dash, not bash and gnome-terminal spawns bash as the interactive shell. While executing, make sure the shebang is correct, and desired one. Also while source-ing, no need for the shebang, you can drop it.

  • 3
    To further clarify: export makes a variable available to subprocesses of the shell that it's exported from (i.e. subshells, commands run from that shell, etc). There is no way to push a variable up to the parent process of the shell. Environment variables always flow down the process tree, never up. – Gordon Davisson Sep 27 '16 at 5:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.