0

I have a script (A) which begins with a #!/bin/sh shebang.

There is another file (B) which doesn't begin with #!/bin/sh; it has some environment variables which I need to set for A to work.

The file B runs fine, and sets the variables when executed from .bash_profile. For example, in my .bash_profile file:

{. /(full_path)/(envname)/(filename).sh;}

When I write same line in my A script file, nothing happens. Why not?

I did echo $env_variable_name; this gives me the correct path when executed via .bash_profile but gives me blank when via my script A. I tried

  • /(full_path)/(envname)/(filename).sh
    
  • SCRIPT_FULL_NAME=$(readlink -f "$0")
    SCRIPT_PATH=$(dirname $SCRIPT_FULL_NAME)
    sh ${SCRIPT_PATH}/(filename).sh
    

What should I do?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
  • 3
    Welcome to Stack Overflow. Please read http://stackoverflow.com/help/how-to-ask for guidelines on how to write questions. For starters: 1. Please format your code. 2. Add more code to your question to help us understand it better and for future readers to benefit from this question. 3. Look for other questions which might help you out already. For example http://stackoverflow.com/questions/8352851/how-to-call-shell-script-from-another-shell-script 4. Format your text, and give us a minimum working example. – Sahil M May 31 '16 at 08:34
  • What's in your `B` file? Make sure it has no bashisms (e.g. `export foo=bar` rather than `foo=bar; export foo`) - or change `A`'s interpreter to `/bin/bash`. You should be able to trace execution using `sh -x A`; that's often helpful in debugging shell scripts. – Toby Speight May 31 '16 at 09:44

1 Answers1

2

There is another file (B) which doesn't begin with #!/bin/sh; it has some environment variables which I need to set for A to work.

In this case you should source you script B in script A. To source a script in sh, the command is:

. /path/to/B.sh

Note that the . is not the beginning of a relative path to script B.

blatinox
  • 833
  • 6
  • 18