[英]Why doesn't “let” work with “sh myscript” on Ubuntu, when it does with “./myscript”?
By running the following script in sh
and ./
mode get different result on Ubuntu 通过在
sh
和./
模式下运行以下脚本,在Ubuntu上可获得不同的结果
#!/bin/bash
x=80
z=90
let "a=$x+$z"
echo $a
Result: 结果:
sh mode
gives me "blank" output, while ./ mode
yields 170. sh mode
给我“空白”输出,而./ mode
产生170。
Any explanation? 有什么解释吗?
./mode
honors the shebang, which specifies that your script be invoked with bash
. ./mode
表示shebang,它指定使用bash
调用脚本。 By contrast, sh mode
explicitly uses sh
instead, ignoring the shebang. 相比之下,
sh mode
显式使用sh
代替,而忽略了shebang。 ( . mode
doesn't run a new shell at all, but executes the commands in the script inside the same shell you're already using interactively). (
. mode
根本不运行新的Shell,而是在您已经交互式使用的同一Shell中执行脚本中的命令)。
let
is a bash extension for compatibility with pre-POSIX shells. let
是bash扩展,用于与POSIX之前的shell兼容。
On a platform (such as Ubuntu) where /bin/sh
is implemented by an ash derivative, very little functionality not specified in the POSIX sh standard is available. 在通过灰分派生实现
/bin/sh
的平台(例如Ubuntu)上,几乎没有POSIX sh标准未指定的功能可用。
To perform arithmetic in a manner compatible with all POSIX-compliant shells (from the initial 1992 publication of the POSIX sh standard), use the following syntax: 要以与所有POSIX兼容外壳兼容的方式执行算术(从POSIX sh标准的1992年最初发布),请使用以下语法:
a=$(( x + z ))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.