算术循环(算术上下文)中的变量引用应该是$ -prefixed? [英] Should variable references be $-prefixed in arithmetic for loops (arithmetic contexts)?

查看:140
本文介绍了算术循环(算术上下文)中的变量引用应该是$ -prefixed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  count = 5 

for((i = count; i> = 0; i--)); ($(i = $ count; i> = 0; i--));
echo $ i

$ b $ ((i = $((count)); i> = 0; i--));
echo$ i
done

((i = $ {count}; i> = 0; i--));
echo$ i
done


echo$ i
完成

哪一个是正确的或首选的?

解决方案

通常,在((...))等算术语境中 code>和 $((...)),仅以名称引用变量, $ prefix (因为您的命令已经对变量 $ i )进行了处理:
$ b ((i = count; i> = 0; i--)); $ b

 
回声$ i
完成




    ((...))
    本身就是一个算术上下文,所以没有必要使用单独的,扩展的算术上下文 - $((count)) - 在里面。
  • 请注意 $ count $ {count} 是等价的,并且在<$ c中包含变量名称 - count $ $ 之后的$ c> {和} 只有必要从后续字符中消除变量名,这些后续字符也可以合法地作为变量名的一部分(不适用于您的命令)。
    As $ {var}




$ - 前缀变量在算术上下文中的引用确实有效,使用它们很少有很好的理由:th使用 $ 在算术评估之前引入额外的展开步骤,这不仅是不必要的,而且会导致不同的行为,如 rici的有用答案



您需要 $ 前缀


    <
    code> $ :谢谢, rici




    • 位置参数: $ 1 $ 2
    • 位置参数的计数: $#

    • 特殊参数: $? $$ $! (还有别的,但通常不是数字 c> man bash )。

    • 如果您需要非零您通过参数展开 提供的默认值 ;例如 $ {count:-2} 默认为 2 if $ count 未设置或为空。

    • 如果您要将变量值用作而不是操作数;例如:

      op ='*'; echo $((2 $ op 2)) - 这不适用于 op



    All of these options below work for setting i to count.

    count=5
    
    for (( i=count; i>=0; i-- )); do 
        echo "$i"
    done
    
    for (( i=$count; i>=0; i-- )); do 
        echo "$i"
    done
    
    for (( i=$((count)); i>=0; i-- )); do 
        echo "$i"
    done
    
    for (( i=${count}; i>=0; i-- )); do 
        echo "$i"
    done
    

    Which is correct or preferred?

    解决方案

    Generally, in arithmetic contexts such as ((...)) and $((...)), reference variables by name only, without the $ prefix (as your commands are already doing with respect to variable $i):

    for (( i=count; i>=0; i-- )); do 
        echo "$i"
    done
    

    • Since ((...)) is an arithmetic context itself, there is no good reason to use a separate, expanding arithmetic context - $((count)) - inside of it.

    • Note that $count and ${count} are equivalent, and enclosing the variable name - count - in { and } after $ is only necessary to disambiguate the variable name from subsequent characters that can also legally be part of a variable name (which doesn't apply to your commands).
      As Gordon Davisson points out, some people choose to always use the ${var} form for visual clarity.

    While $-prefixed variable references in arithmetic contexts do work, there is rarely a good reason to use them: the use of $ introduces an extra expansion step before the arithmetic evaluation, which is not only unnecessary, but can result in different behavior, as explained in rici's helpful answer.

    The only cases where you need the $ prefix:

    • To reference positional and special parameters (variables) that can only ever be referenced with $: Thanks, rici.

      • Positional parameters: $1, $2, ...
      • The count of positional parameters: $#
      • Special parameters: $?, $$, $! (there are others, but they are not generally numeric - see chapter Special Parameters in man bash).
    • If you need a nonzero default value that you provide via a parameter expansion; e.g., ${count:-2} defaults to 2 if $count is unset or empty.

    • If you want to use a variable value as an operator rather than operand; e.g.:
      op='*'; echo $(( 2 $op 2 )) - this wouldn't work with just op.

    这篇关于算术循环(算术上下文)中的变量引用应该是$ -prefixed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆