为什么带有“#!/bin/sh”shebang 的脚本可以使用仅 bash 的功能?
Why can a script with a "#!/bin/sh" shebang use bash-only features?
当我运行下面的脚本时:
#!/bin/sh
declare path="my.path"
if [[ "$path" =~ \. ]] ; then
echo "yes"
else
echo "no"
fi
我打印了 yes
。
但是如果我没记错的话,declare
和 [[ ]]
在 POSIX shell 中不可用;那么这个脚本怎么不抛出任何错误呢?
我 运行 git 上 Windows 的脚本,它能解释这种奇怪的行为吗?
/bin/sh
是 link 到你实际的 shell。这并不意味着你是 运行 纯粹的 POSIX。 Git for windows homepage 非常清楚地表明您是 运行 bash
,您的 git-bash
标签也是如此。
即使在 Linux,/bin/sh
仍然可以是 bash:
ls -la /bin/sh
lrwxrwxrwx. 1 root root 4 Mar 27 09:33 /bin/sh -> bash
当我运行下面的脚本时:
#!/bin/sh
declare path="my.path"
if [[ "$path" =~ \. ]] ; then
echo "yes"
else
echo "no"
fi
我打印了 yes
。
但是如果我没记错的话,declare
和 [[ ]]
在 POSIX shell 中不可用;那么这个脚本怎么不抛出任何错误呢?
我 运行 git 上 Windows 的脚本,它能解释这种奇怪的行为吗?
/bin/sh
是 link 到你实际的 shell。这并不意味着你是 运行 纯粹的 POSIX。 Git for windows homepage 非常清楚地表明您是 运行 bash
,您的 git-bash
标签也是如此。
即使在 Linux,/bin/sh
仍然可以是 bash:
ls -la /bin/sh
lrwxrwxrwx. 1 root root 4 Mar 27 09:33 /bin/sh -> bash