0

I want to send the path to a bash script which sources some environmental variables as an argument to another bash script to run it and use the environmental variables. It works well with no arguments if I hard coded the path to the bash script to run it works and I can retrieve the environmental variables in the main script. the problem happens when I send the path as an argument it does not want to run it. for example if the path is /path/script.bash and I send the path as an argument I get the error that /path/env_set: No such a file or directory I run the script by this line

. $1 (this doesn't work)
. /path/script.bash (this works)

if I use

bash -c $1 

the bash file runs but it does not set the environmental variables to use it in the main script

I don't know why env_set replaces the script name when I use arguments. Is there any approach to achieve this or any work around to achieve my goal?

3
  • 2
    I tested . $1 in bash on my computer and it runs as expected. I advise you to put double quotes around "$1" for the case the path to your script contains spaces. Commented Jun 1, 2021 at 9:03
  • You should tell us: what is env_set and show a little bit more of your code. Try to make a minimal reproducible example, as usual on SO. Commented Jun 1, 2021 at 9:08
  • 2
    Also, putting set -x at the beginning of the script to get an execution trace may help clarify what's going on. Commented Jun 1, 2021 at 9:23

2 Answers 2

1

It sounds like the problem could be either with your quoting, or with relative paths.

Quoting isn't just about spaces, it's also about pathname expansion (ie. []?* characters).

Do

. "$1"

(not . $1)

And remember, if you're giving a relative path for the environment script (or that script uses some relative paths), you will have a problem. Those paths are relative to the pwd - which is wherever you happen to be when you execute the main script (not where any of the script files themselves happen to be located, for example).

Finally, you can debug this problem by throwing echo at the start, and running the script (if it's safe to do that): echo . "$1" exit # Add exit here if you don't want to run w/o the vars. Now you can see what you're actually trying to source.

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

Comments

0

In script 1, in your main code, you can call and run script 2, . ./script 2

The first . stands for current shell, and the second . for current directory.

which will create the environment variables for you, and configure any other settings as well in the same terminal. Afterwards when script 2 has finished running, script 1 would continue to run, and your environment variables which was created in script 2, will be accessible for script 1 to use in the same session.

Comments

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.