Unable to export the variable through script file

2019-01-08 02:27发布

问题:

This question already has an answer here:

  • Global environment variables in a shell script 5 answers

I am trying to export a variables through myDeploy.sh but the export is not getting set. When i am echoing it is not echoing. However, when i set the variable explicitly on command it sets properly and echoes too.Below is the snippet of my code.

myDeploy.sh

 #!/bin/bash

# export the build root

export BUILD_ROOT=/tibco/data/GRISSOM2

export CUSTOM1=/tibco/data/GRISSOM2/DEPLOYMENT_ARTIFACTS/common/MDR_ITEM_E1/rulebase

export CLEANUP=$BUILD_ROOT/DEPLOYMENT_ARTIFACTS/common

cd $BUILD_ROOT/DEPLOYMENT_ARTIFACTS/common

When I echoes echo $BUILD_ROOT it is not echoing the path for me. But when I do it explicitly on command prompt like

[root@krog3-rhel5-64 GRISSOM2]# export BUILD_ROOT=/tibco/data/GRISSOM2

It sets properly and echoes too. What am I missing?

回答1:

Running your script like

. ./script

or

source script

would execute your script in the current shell context (without creating a subshell) and the environment variables set within the script would be available in your current shell.

From the manual:

. filename [arguments]

Read and execute commands from the filename argument in the current shell context. If filename does not contain a slash, the PATH variable is used to find filename. When Bash is not in POSIX mode, the current directory is searched if filename is not found in $PATH. If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional parameters are unchanged. The return status is the exit status of the last command executed, or zero if no commands are executed. If filename is not found, or cannot be read, the return status is non-zero. This builtin is equivalent to source.



标签: linux bash