$ @和$ *之间的差异 [英] Difference between $@ and $*

查看:138
本文介绍了$ @和$ *之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个简单的脚本,该脚本可以使用任意数量的参数来演示$@$*之间的区别:

I have wrote a simple script that takes in any number of parameters to demonstrate the difference between $@ and $*:

#!/bin/bash                                                                
echo "double quoted $*     $@"
echo 'single quoted $*     $@'

在CLI上我做了

$./stuff.sh a b c d e f dfs 

这就是打印出来的

double quoted a b c d e f dfs     a b c d e f dfs
single quoted $*     $@

由于它们相同,是否意味着$@等于$*?还是我想念的一点?

Since they are identical does that mean $@ equals to $*? Or is there a point I am missing?

推荐答案

来自

*

从一个开始扩展到位置参数.当...的时候 扩展出现在双引号内,它扩展为一个单词 每个参数的值由的第一个字符分隔 IFS特殊变量.也就是说,"$ *"等效于"$ 1c $ 2c…", 其中c是IFS变量值的第一个字符.如果 IFS未设置,参数用空格分隔.如果IFS为空, 这些参数是在不插入分隔符的情况下进行连接的.

Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c…", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.

@

从一个开始扩展到位置参数.当...的时候 扩展出现在双引号内,每个参数扩展为一个 单独的单词.也就是说,"$ @"等效于"$ 1""$ 2"…….如果 双引号扩展发生在一个词内,扩展 第一个参数与原始文件的开头部分结合在一起 单词,最后一个参数的扩展与最后一个连接 原始单词的一部分.如果没有位置参数, "$ @"和$ @扩展为空(即它们被删除了).

Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" …. If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).

一个更好的例子:

$ d=(a b c)
$ for i in "${d[*]}"; do echo $i; done <---- it is a field all together
a b c
$ for i in "${d[@]}"; do echo $i; done <---- each item is a different field
a
b
c

这篇关于$ @和$ *之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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