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?

2
  • 3
    Welcome to Stack Overflow. Please read 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 stackoverflow.com/questions/8352851/… 4. Format your text, and give us a minimum working example. Commented May 31, 2016 at 8: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. Commented May 31, 2016 at 9:44

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Also using the keyword source (which . is an alias for) might be more readable.
Wow, I didn't know that. Thanks. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.