Windows 中的 bash 与 Linux 中的 bash 行为不同

Yos*_*ssi -1 bash shell-script

我在 Windows 10 上使用以下版本的 bash:

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

我从在 Linux 上运行它的人那里收到了一个脚本。在 Windows 上运行时,我得到了不同的结果。

脚本是test.sh

#!/bin/bash
set -x
( . settings.sh ; . constants.js ) > output.js
Run Code Online (Sandbox Code Playgroud)

settings.sh

TEST_URL="https://myurl.com"
Run Code Online (Sandbox Code Playgroud)

constants.js

cat << EOF
export class Output {
}
Constants.Url = "$TEST_URL";
EOF
Run Code Online (Sandbox Code Playgroud)

output.js 在 Linux 上如下所示:

export class Output {
}
Constants.Url = "https://myurl.com";
Run Code Online (Sandbox Code Playgroud)

该脚本未在 Windows 上运行。我将其修改为:

#!/bin/bash
set -x
( ./settings.sh ; ./constants.js ) > output.js
Run Code Online (Sandbox Code Playgroud)

在 Windows 上:

export class Output {
}
Constants.Url = "";
Run Code Online (Sandbox Code Playgroud)

知道如何编写脚本以便在 Windows 上获得与在 Linux 上相同的结果吗?

Jde*_*eBP 5

( ./settings.sh ; ./constants.js )

这是在运行 Bourne Again shell 作为脚本解释器的子进程中运行的。它产生一个子shell,它依次连续运行两个脚本作为子进程。

第一个子脚本设置一个 shell 变量。它甚至不会尝试将其从 shell 变量导出到环境变量。但即使,那也行不通。子进程只能影响它自己及其子进程的环境。它不能影响其父(子)shell 或其祖父。第二个子脚本没有变量,并产生带有空字符串的输出。

第一个脚本需要源代码,而不是作为子进程运行。

( . ./settings.sh ; ./constants.js )