gpt4 book ai didi

linux - bash 中变量的空格和花括号

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:02 26 4
gpt4 key购买 nike

有没有办法让 ${VAR} 像用双引号引起来一样展开?

这不是我想看到的:

% A="some spaces in there"
% touch ${A}
% ls -1
in
some
spaces
there

当然,我可以使用像 "$VAR" 这样的典型符号。但这在引用文本等中使用引号时很麻烦。我想知道是否有一种扩展 ${...} 符号的方法可以将 ${...} 视为 "${...}" 本身不使用双引号?

最佳答案

没有关注 accepted best practice (并了解 shell 真正 是如何工作的)很可能会咬你一口。反复。长着被丧尸病毒感染的獠牙。一些可以提供帮助的事情:

  • 您可以对单个参数的不同部分使用不同的引号,只要开始和结束引号彼此相邻即可:

    $ printf '%q\n' "foo 'bar' baz"'nuu "boo" zoo'
    foo\ \'bar\'\ baznuu\ \"boo\"\ zoo
  • 您可以在子 shell 中设置 IFS 以避免搞砸您的整个脚本:

    $ a="some spaces in there"
    $ (IFS= && touch ${a})
    $ ls -1
    some spaces in there

关于linux - bash 中变量的空格和花括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27040682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com