可变$ x $ n算术扩展
我遇到了这个教程示例,尤其是行sum = $ = $(($ sum+$ x $ n))
。 $ x $ n
特别是什么意思?我想引用$ x但未分配(未宣布/未设置变量),不是吗?
https://linuxhint.com/bash_eval_command/
#!/bin/bash
# Initialize the variable $sum with the value 0
sum=0
# Declare a for loop that will iterate for 4 times
for n in {1..4}
do
# Create four variables using eval command
eval x$n=$n
# Add the values of the variable with $sum
sum=$(($sum+$x$n))
done
# Assign `echo` command with string into a variable
command="echo 'The result of the sum='"
# `eval` command print the sum value using variables
eval $command $sum
I come across this tutorial example, especial the line sum=$(($sum+$x$n))
. What does $x$n
mean in particular? I suppose $x is referenced but not assigned (undeclared/unset variable), isn't it?
https://linuxhint.com/bash_eval_command/
evaltest3.sh
#!/bin/bash
# Initialize the variable $sum with the value 0
sum=0
# Declare a for loop that will iterate for 4 times
for n in {1..4}
do
# Create four variables using eval command
eval x$n=$n
# Add the values of the variable with $sum
sum=$(($sum+$x$n))
done
# Assign `echo` command with string into a variable
command="echo 'The result of the sum='"
# `eval` command print the sum value using variables
eval $command $sum
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(不是直接回答您的问题,而是提供替代方案)
,因为Bash Arithmetic允许我们提供变量“不使用参数扩展语法” [ ref ](即没有
$
),我们可以写出优势是bash bash and bash and bash and bash and bash and bash andley and bash and bash andlen offeres数量零。
(Does not directly answer your question, but offers an alternative)
Since bash arithmetic allows us to provide variables "without using the parameter expansion syntax" [Ref] (i.e. without the
$
), we can writeThe advantage is that bash handles empty or unset variables as the number zero.