UNIX中的$@和$*有什么区别?当在脚本中回显时,它们似乎产生相同的输出.
一个区别在于它们如何处理输出上的IFS变量.
原文链接:https://www.f2er.com/bash/386635.html#!/bin/sh echo "unquoted asterisk " $* echo "quoted asterisk $*" echo "unquoted at " $@ echo "quoted at $@" IFS="X" echo "IFS is now $IFS" echo "unquoted asterisk " $* echo "quoted asterisk $*" echo "unquoted at " $@ echo "quoted at $@"
如果你这样运行:./demo abc def ghi,你得到这个输出:
unquoted asterisk abc def ghi quoted asterisk abc def ghi unquoted at abc def ghi quoted at abc def ghi IFS is now X unquoted asterisk abc def ghi quoted asterisk abcXdefXghi unquoted at abc def ghi quoted at abc def ghi
请注意,(仅)“IFS”更改为“X”后,“引用的星号”行显示每个“单词”之间的X.如果IFS的值包含多个字符,则仅使用第一个字符用于此目的.
此功能也可用于其他阵列:
$array=(123 456 789) $saveIFS=$IFS; IFS="|" $echo "${array[*]}" 123|456|789 $IFS=$saveIFS