简体   繁体   English

bash输出星号和引号不正确

[英]bash outputs star and quotation mark incorrectly

so I have this bash function: 所以我有这个bash函数:

function xyz(){
  echo $@
}

now when I run 现在,当我跑步

xyz whaat  "loool * hahhaa"

instead of echoing whaat "loool * hahhaa" 而不是呼whaat "loool * hahhaa"

it insteads echoes: 它代替了回声:

whaat loool all default hahhaa

First of all the quotation marks got stripped off. 首先,引号被删除。 Secondly the * got replaced by "all default" . 其次,*被“ all default”代替。 This is because the current directory has 2 folders called "all" and "default" hence it think * refers to all directory in the current directory 这是因为当前目录有2个名为“ all”和“ default”的文件夹,因此它认为*表示当前目录中的所有目录

Is there a way to modify my function so that the output becomes the intended whaat "loool * hahhaa" accordingly (including the quotation marks and *) 有没有办法修改我的功能,以便输出相应地变为预期的whaat "loool * hahhaa" (包括引号和*)

I tried doing ${@} "$@" and "${@}" to no avail 我尝试执行${@} "$@""${@}"无济于事

The quotation marks are really gone, so don't be expecting anything to put them back. 引号真的消失了,所以不要指望有什么东西能把它们放回去。 Bash removed them as part of parsing the command line (search for "quote removal" in man bash ). Bash在解析命令行(在man bash搜索“引用删除”)的过程中将其删除。

However, either echo "$@" or echo "${@}" will avoid the glob expansion (replacing * with the directory listing). 但是, echo "$@"echo "${@}"可以避免全局扩展(用目录列表替换* )。

You practically never want $@ . 您几乎不需要$@ Best to get in the habit of writing "$@" . 最好养成写"$@"的习惯。

If you want to see a quoted version of the arguments, you can a bash-extension to printf: 如果要查看参数的带引号的版本,可以对bash进行bash扩展:

xyz() {
  printf "%q " "$@"
  printf "\n"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM