1

I have a bash script, from which I need to start another script with source. This is working fine, but I also need to pass the 2nd script parameters.

Example:

source /usr/local/scripts/parallel.sh test --gnu

So I need to start parallel.sh with a given data-source file called test, and I also need to assign a parameter --gnu at the end. But it is not recognizing the file and the parameter.

6
  • Why do you need to source it? Can't you just call it normally? Commented Mar 9, 2016 at 13:08
  • you mean with ./path/to/script? Commented Mar 9, 2016 at 13:26
  • Yes. Sourcing is used when you want to import variables of functions from the script. Commented Mar 9, 2016 at 13:28
  • Sourcing will mean that it runs in the same process space so any variables will be visible and potentially mutable. Also if your process crashes it'll crash the whole thing, not just parallel.sh Running it, on the other hand, will put it into a separate process, variable will only be seen in parallel.sh if you've exported them and any env var you change will be lost once control returns to your calling script. Commented Mar 9, 2016 at 13:31
  • Are your test --gnu parameters being passed to parallel.sh? If so, that should work fine. You should be able to reference them as ${1} and ${2} within parallel.sh Commented Mar 9, 2016 at 13:33

1 Answer 1

1

The source command is likely not what you're looking for.

When a script is run using source it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as filename, then a separate subshell (with a completely separate set of variables) would be spawned to run the script.

So, unless you need access to variables or functions inside parallel.sh, just call it directly:

/usr/local/scripts/parallel.sh test --gnu

As long as the script is executable (chmod +x /usr/local/scripts/parallel.sh) and set up to work with the options you're passing ($1 will contain "test" and $2 will contain "--gnu") it should work just fine.

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

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.