简体   繁体   English

为什么在Ubuntu上“ let”不能与“ sh myscript”一起使用,而对“ ./myscript”却可以呢?

[英]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? 有什么解释吗?

Shell Selection via Invocation Method 通过调用方法选择外壳

./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中执行脚本中的命令)。


Why Shell Selection Matters 为什么选择壳牌很重要

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM