【发布时间】:2018-12-05 17:43:46
【问题描述】:
当我运行以下脚本时:
#!/bin/sh
declare path="my.path"
if [[ "$path" =~ \. ]] ; then
echo "yes"
else
echo "no"
fi
我打印了yes。
但如果我没记错的话,declare 和 [[ ]] 在 POSIX shell 中不可用;那么这个脚本怎么没有抛出任何错误呢?
我在 git for Windows 上运行脚本,它可以解释这种奇怪的行为吗?
【问题讨论】:
-
sh可以是任何 POSIX 兼容的 shell。该脚本可能适用于 GitBash/WSL/macOS/OpenBSD(因为sh由 bash 或 ksh 提供)但不适用于 Debian/Ubuntu/Android/FreeBSD。如果您坚持使用 POSIX 功能,它将适用于每个系统。如果您指定bash,它将适用于每个安装了 bash 的系统。