【发布时间】:2019-02-01 06:42:30
【问题描述】:
$argv[1]和$1有什么区别?$argv[1]和$argv[$1]哪个对?
我尝试了以下代码并获得了如下所述的输出:
1)
#!/bin/bash
echo "The user $1 is logged on to the system on"
运行为:bash logs.sh home
输出:The user home is logged on to the system on
2)
#!/bin/bash
echo "The user $argv[1] is logged on to the system on"
运行为:bash logs.sh home
输出:The user [1] is logged on to the system on
3)
#!/bin/bash
echo "The user $argv[$1] is logged on to the system on"
运行为:bash logs.sh home
输出:The user [home] is logged on to the system on
请澄清这些。
【问题讨论】:
-
为什么这个标签是
linux-kernel?bash也可以在其他操作系统上运行 -
在 bash/shell 中没有名为
argv的变量,而您的测试只是暴露了它。 (在您的测试中,将$argv替换为空字符串,结果将是一致的)。