AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 484285
Accepted
De Novo
De Novo
Asked: 2018-11-27 12:12:22 +0800 CST2018-11-27 12:12:22 +0800 CST 2018-11-27 12:12:22 +0800 CST

`$@` 与 `$*` 行为 [重复]

  • 772
这个问题在这里已经有了答案:
$* 和 $@ 有什么区别? (9 个回答)
a="$@" 的意外结果 (1 个回答)
3年前关闭。

bash 手册说:

关于:$*

当扩展发生在双引号内时,它扩展为单个单词,每个参数的值由IFS特殊变量的第一个字符分隔。也就是说,"$*"相当于"$1c$2c...",其中是变量c值的第一个字符。IFS

关于:$@

当扩展出现在双引号内时,每个参数都扩展为一个单独的单词。也就是说,"$@"相当于"$1" "$2" ....

IFS如果变量值的第一个字符实际上是一个空格,我似乎无法想出这两个特殊参数会产生不同行为的示例。任何人都可以为我提供一个IFS他们会产生不同行为的例子(同样,不改变)吗?

我自己的测试仍然让我有些困惑,如下所示:

#!/usr/bin/env bash
# File: test.sh
# set foo and bar in the global environment to $@ and $*

test_expansion () {
  foo="$@"
  bar="$*"
}

现在测试:

. test.sh
test_expansion a b c d
# foo is $@
# bar is $*

for e in "$foo"; do
  echo "$e"
done
# a b c d

for e in "$bar"; do
  echo "$e"
done
# a b c d
bash arguments
  • 3 3 个回答
  • 96 Views

3 个回答

  • Voted
  1. Best Answer
    Lewis M
    2018-11-27T12:22:28+08:002018-11-27T12:22:28+08:00

    当您在命令行上传递的参数中包含 IFS 字符(例如,带有空格的参数)时,就会出现差异。要查看差异,请查看此脚本:

    #!/bin/bash
    
    echo 'Here is $*'
    for x in "$*"; do
        echo "  !${x}!"
    done
    
    echo ""
    echo 'And here is $@'
    for x in "$@"; do
        echo "  !${x}!"
    done
    
    exit 0
    

    现在,看看传入带空格的参数时的区别。

    ./testspace01.sh "This is" a test
    Here is $*
      !This is a test!
    
    And here is $@
      !This is!
      !a!
      !test!
    

    更新 啊,将命令行中传入的内容分配给变量带来了它自己的小怪癖。:)

    请记住,在命令行中传递的所有内容都是一个数组。因此,将数组分配给一个字符串会给您带来与对数组签名不同的东西。而且,根据您使用的是星号还是星号,处理数组的方式会有所不同。这是我的脚本的更新版本。

    #!/bin/bash
    
    s_star="$*"
    echo 'Here is s_star'
    for x in "${s_star}"; do
        echo "  !${x}!"
    done
    
    a_star=("$*")
    echo ""
    echo 'Here is a_star'
    for x in "${a_star}"; do
        echo "  !${x}!"
    done
    
    s_at="$@"
    echo ""
    echo 'Here is s_at'
    for x in "${s_at}"; do
        echo "  !${x}!"
    done
    
    a_at=("$@")
    echo ""
    echo 'Here is a_at (using star)'
    for x in "${a_at[*]}"; do
        echo "  !${x}!"
    done
    
    echo ""
    echo 'Here is a_at (using at)'
    for x in "${a_at[@]}"; do
        echo "  !${x}!"
    done
    
    exit 0
    

    这是输出:

    ./testspace02.sh "This is" a test
    Here is s_star
      !This is a test!
    
    Here is a_star
      !This is a test!
    
    Here is s_at
      !This is a test!
    
    Here is a_at (using star)
      !This is a test!
    
    Here is a_at (using at)
      !This is!
      !a!
      !test!
    

    如您所见,存在不同的行为。

    • 3
  2. choroba
    2018-11-27T12:22:45+08:002018-11-27T12:22:45+08:00

    尝试这个:

    #!/bin/bash
    show-difference () {
        for e in "$@" ; do
            printf '<%s>\n' "$e"
        done
        for e in "$*" ; do
            printf '[%s]\n' "$e"
        done
    }
    
    show-difference {a..h}
    

    输出:

    <a>
    <b>
    <c>
    <d>
    <e>
    <f>
    <g>
    <h>
    [a b c d e f g h]
    

    "$*"是单个词,同时"$@"将所有参数扩展为单个词。此外,"$@"即使参数包含 IFS 的第一个字符,也能正常工作。

    • 3
  3. Kusalananda
    2018-11-27T12:26:43+08:002018-11-27T12:26:43+08:00
    set -- "hello there" bumblebee
    
    printf '%s\n' "$@"
    printf '%s\n' "$*"
    

    结果:

    hello there
    bumblebee
    

    其次是

    hello there bumblebee
    

    这表明在生成单引号字符串的同时生成单独引用元素"$@"的列表。"$*"

    使用bash,这也可以通过一个带有多个命令行参数的简短 shell 脚本来说明:

    #!/bin/bash
    
    IFS='_'
    
    atarray=( "$@" )
    stararray=( "$*" )
    
    printf 'at: %s\n' "${atarray[@]}"
    printf 'star: %s\n' "${stararray[@]}"
    

    运行它:

    $ bash ./script.sh "one two" three four
    at: one two
    at: three
    at: four
    star: one two_three_four
    

    这也表明 using"$*"将使用中的值$IFS(仅第一个字符)来分隔扩展产生的字符串中的元素。

    • 0

相关问题

  • 通过命令的标准输出以编程方式导出环境变量[重复]

  • 从文本文件传递变量的奇怪问题

  • 虽然行读取保持转义空间?

  • `tee` 和 `bash` 进程替换顺序

  • 运行一个非常慢的脚本直到它成功

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    ssh 无法协商:“找不到匹配的密码”,正在拒绝 cbc

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    如何卸载内核模块“nvidia-drm”?

    • 13 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Wong Jia Hau ssh-add 返回:“连接代理时出错:没有这样的文件或目录” 2018-08-24 23:28:13 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve