gpt4 book ai didi

linux - : a=1, let a=1, ((a=1)) 这三种赋值方式有区别吗

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:40 24 4
gpt4 key购买 nike

考虑三种赋值方式:

#!/bin/bash

a=1
let a=1
((a=1))

三个赋值是否完全相等?

最佳答案

如果你的 shell 有非 POSIX 扩展(即 ksh 或 bash),它们是完全等价的:a=1 是一个字符串赋值,而 let a=1(( a=1 )) 是数字赋值,但是存储在 a 中的是一个字符串,表示每个中的数字 1案例。

$(( )) 是创建数学上下文的符合 POSIX 的方法。 (( ))let 相比之下,不是 由 POSIX 指定,但是在一些超出标准的 shell 中可用的扩展。


现在,考虑一下:

b=2

# result | posix? | type | comments
# --------+---------+--------------------+----------------
a=b # a=b | true | string assignment |
let a=b # a=2 | false | numeric assignment | ancient non-POSIX syntax
a=$((b)) # a=2 | true | numeric assignment | modern POSIX syntax
((a=b)) # a=2 | false | numeric assignment | modern non-POSIX extension

关于linux - : a=1, let a=1, ((a=1)) 这三种赋值方式有区别吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32194008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com