-1

I am using the following version of bash on Windows 10:

GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)

I received a script from somebody who is running it on Linux. When running on Windows, I get a different result.

The script is test.sh:

#!/bin/bash
set -x
( . settings.sh ; . constants.js ) > output.js

settings.sh:

TEST_URL="https://myurl.com"

constants.js:

cat << EOF
export class Output {
}
Constants.Url = "$TEST_URL";
EOF

output.js on Linux looks as following:

export class Output {
}
Constants.Url = "https://myurl.com";

The script didn't run on Windows. I modified it to:

#!/bin/bash
set -x
( ./settings.sh ; ./constants.js ) > output.js

And on Windows:

export class Output {
}
Constants.Url = "";

Any idea how to write the script in order to get on Windows the same result as on Linux?

closed as off-topic by Michael Homer, roaima, Kusalananda Apr 12 at 14:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Michael Homer, roaima, Kusalananda
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    By bash on Windows, do you mean the Windows Subsystem for Linux (WSL) (aka the Ubuntu app in the Windows store and similar) or Cygwin or Git Bash or something else? – muru Apr 12 at 8:27
  • 1
    That test.sh script shouldn't work anywhere; are you sure it is verbatim identical in both places? It doesn't have an extra / on line 3? – Michael Homer Apr 12 at 8:32
  • 2
    I do not reproduce your claimed Linux behaviour. It is also incorrect behaviour. The claimed Windows behaviour is the correct behaviour for the scripts presented. I deduce that when you tested this on Linux, you already had TEST_URL in the interactive shell's environment. Test properly on Linux, and you will find that there is no difference in behaviour. Your question should be how to make this behaviour what you want on either platform. – JdeBP Apr 12 at 8:33
  • 1
    @muru It is Git Bash – Yossi Apr 12 at 8:34
  • 1
    Presumably it should be . ./settings.sh, to source that file, – muru Apr 12 at 8:37
5
( ./settings.sh ; ./constants.js )

This is running within a child process running the Bourne Again shell as a script interpreter. It spawns a subshell, which in turn serially runs two scripts as child processes.

The first child script sets a shell variable. It does not even attempt to export it from a shell variable to an environment variable. But even if it had, that would not have worked. The child process can only affect its own environment and that of its children. It cannot affect its parent (sub-)shell, or its grandparent. The second child script does not have the variable, and yields the output with the empty string.

The first script needs to be sourced, not run as a child process.

( . ./settings.sh ; ./constants.js )
  • Thanks! I forgot that I modified the linux script since it didn't run on windows. Updated the question, and your answer does what I needed to do... – Yossi Apr 12 at 9:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.