2

I'm calling Uncle. I'm attempting to manipulate variables that have hard coded values in a second bash script I am calling. I have no control over the script and am building a wrapper around it to adjust some build behavior before it finally kicks off a yocto build. I'm not sure what else to try after reading and trying numerous examples.

Examples of the situation:

build.sh calls build2.sh

IS_DEV=1 ./build2.sh #trying to override value

build2.sh

IS_DEV=0 # hardcoded value
echo $IS_DEV
# always results in 0.

I have also tried export IS_DEV=1 before calling build2.sh.

I'm sure this is pretty simple, but I cannot seem to get this to work. I appreciate any assistance. Is this possible? I'm using GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu) on Ubuntu 16.04.4 LTS.

Oh, I have also tried the sourcing technique with no luck.

IS_DEV=1 . ./build2.sh
IS_DEV=1 source ./build2.sh

Where am I getting this wrong?

Much appreciated.

7
  • This question, and the answers, would indicate you may be out of luck unless you can get support for this in the second script stackoverflow.com/questions/4609668/… Commented Jun 15, 2018 at 19:43
  • Possible duplicate of override variable in bash script from command line Commented Jun 15, 2018 at 19:44
  • @BrandonMiller yes I have looked at that post and tried the examples, no go. I'll see if I can gain the ability to tweak the second script. thx. Commented Jun 15, 2018 at 19:48
  • 1
    I'd always have written your build2 to have : "${IS_DEV:=0}", making 0 explicitly an overridable default. (BTW, all-caps variables are used by the shell and POSIX utilities for their own use; the namespace of variables with at least one lower-case letter are reserved for application use and guaranteed not to modify the shell's behavior; see relevant spec @ pubs.opengroup.org/onlinepubs/9699919799/basedefs/…, fourth paragraph) Commented Jun 15, 2018 at 20:07
  • @CharlesDuffy I wish I could directly edit the called script and clean it up, but I cannot. :( Commented Jun 15, 2018 at 20:09

1 Answer 1

4

If you can't modify the script, execute a modified version of it.

sed 's/^IS_DEV=0 /IS_DEV=1 /' build2.sh | sh

Obviously, pipe to bash if you need Bash semantics instead of POSIX sh semantics.

If the script really hard-codes a value with no means to override it from the command line, modifying that script is the only possible workaround. But the modification can be ephemeral; the above performs a simple substitution on the script, then passes the modified temporary copy through a pipe to a new shell instance for execution. The modification only exists in the pipeline, and doesn't affect the on-disk version of build2.sh.

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

3 Comments

See also tangentially Difference between sh and bash
thank you! this saved me. I've never used sed, now I will. thx again.
hey guys, the second part of this is that after using sed to rewrite an in-memory copy of the second script, I also need to "forward along" a big command line to the same script. what is the approach for that? example: sed 's/^IS_DEV=0 /IS_DEV=1 /' build2.sh | bash -d -a 1 --logs I would want all the "additional" cmd line args to pass to build2.sh and not as args to the bash shell.

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.