首页 文章

在bash shell脚本中传播所有参数

提问于
浏览
630

我正在编写一个调用另一个脚本的非常简单的脚本,我需要将参数从当前脚本传播到我正在执行的脚本 .

例如,我的脚本名称是 foo.sh 并调用 bar.sh

foo.sh:

bar $1 $2 $3 $4

如何在不明确指定每个参数的情况下执行此操作?

8 回答

  • 26

    使用 "$@" (适用于所有POSIX兼容机) .

    [...],bash具有“$ @”变量,该变量扩展为由空格分隔的所有命令行参数 .

    Bash by example起 .

  • 6

    我的SUN Unix有很多限制,甚至“$ @”也没有被解释为所希望的 . 我的解决方法是$ {@} . 例如,

    #!/bin/ksh
    find ./ -type f | xargs grep "${@}"
    

    顺便说一句,我必须有这个特殊的脚本,因为我的Unix也不支持grep -r

  • 1

    对于bash和其他类似Bourne的shell:

    java com.myserver.Program "$@"
    
  • 455

    工作正常,除非您有空格或转义字符 . 在这种情况下我找不到捕获参数的方法并发送到脚本内的ssh .

    这可能很有用但很难看

    _command_opts=$( echo "$@" | awk -F\- 'BEGIN { OFS=" -" } { for (i=2;i<=NF;i++) { gsub(/^[a-z] /,"&@",$i) ; gsub(/ $/,"",$i );gsub (/$/,"@",$i) }; print $0 }' | tr '@' \' )
    
  • 1045

    如果您真的希望您的参数传递相同,请使用 "$@" 而不是plain $@ .

    注意:

    $ cat foo.sh
    #!/bin/bash
    baz.sh $@
    
    $ cat bar.sh
    #!/bin/bash
    baz.sh "$@"
    
    $ cat baz.sh
    #!/bin/bash
    echo Received: $1
    echo Received: $2
    echo Received: $3
    echo Received: $4
    
    $ ./foo.sh first second
    Received: first
    Received: second
    Received:
    Received:
    
    $ ./foo.sh "one quoted arg"
    Received: one
    Received: quoted
    Received: arg
    Received:
    
    $ ./bar.sh first second
    Received: first
    Received: second
    Received:
    Received:
    
    $ ./bar.sh "one quoted arg"
    Received: one quoted arg
    Received:
    Received:
    Received:
    
  • 17

    我意识到这已经得到了很好的解答,但这里是“$ @”$ @“$ *”和$ *之间的比较

    测试脚本的内容:

    # cat ./test.sh
    #!/usr/bin/env bash
    echo "================================="
    
    echo "Quoted DOLLAR-AT"
    for ARG in "$@"; do
        echo $ARG
    done
    
    echo "================================="
    
    echo "NOT Quoted DOLLAR-AT"
    for ARG in $@; do
        echo $ARG
    done
    
    echo "================================="
    
    echo "Quoted DOLLAR-STAR"
    for ARG in "$*"; do
        echo $ARG
    done
    
    echo "================================="
    
    echo "NOT Quoted DOLLAR-STAR"
    for ARG in $*; do
        echo $ARG
    done
    
    echo "================================="
    

    现在,使用各种参数运行测试脚本:

    # ./test.sh  "arg with space one" "arg2" arg3
    =================================
    Quoted DOLLAR-AT
    arg with space one
    arg2
    arg3
    =================================
    NOT Quoted DOLLAR-AT
    arg
    with
    space
    one
    arg2
    arg3
    =================================
    Quoted DOLLAR-STAR
    arg with space one arg2 arg3
    =================================
    NOT Quoted DOLLAR-STAR
    arg
    with
    space
    one
    arg2
    arg3
    =================================
    
  • 74

    如果在带有其他字符的带引号的字符串中包含 $@ ,则当存在多个参数时,行为非常奇怪,只有第一个参数包含在引号内 .

    例:

    #!/bin/bash
    set -x
    bash -c "true foo $@"
    

    产量:

    $ bash test.sh bar baz
    + bash -c 'true foo bar' baz
    

    但首先分配给另一个变量:

    #!/bin/bash
    set -x
    args="$@"
    bash -c "true foo $args"
    

    产量:

    $ bash test.sh bar baz
    + args='bar baz'
    + bash -c 'true foo bar baz'
    
  • 1
    #!/usr/bin/env bash
    while [ "$1" != "" ]; do
      echo "Received: ${1}" && shift;
    done;
    

    只是认为在尝试测试args如何进入脚本时可能会更有用

相关问题