Search code examples
bashcommand-line-arguments

Propagate all arguments in a Bash shell script


I am writing a very simple script that calls another script, and I need to propagate the parameters from my current script to the script I am executing.

For instance, my script name is foo.sh and calls bar.sh.

foo.sh:

bar $1 $2 $3 $4

How can I do this without explicitly specifying each parameter?


Solution

  • Use "$@" instead of plain $@ if you actually wish your parameters to be passed the same.

    Observe:

    $ cat no_quotes.sh
    #!/bin/bash
    echo_args.sh $@
    
    $ cat quotes.sh
    #!/bin/bash
    echo_args.sh "$@"
    
    $ cat echo_args.sh
    #!/bin/bash
    echo Received: $1
    echo Received: $2
    echo Received: $3
    echo Received: $4
    
    $ ./no_quotes.sh first second
    Received: first
    Received: second
    Received:
    Received:
    
    $ ./no_quotes.sh "one quoted arg"
    Received: one
    Received: quoted
    Received: arg
    Received:
    
    $ ./quotes.sh first second
    Received: first
    Received: second
    Received:
    Received:
    
    $ ./quotes.sh "one quoted arg"
    Received: one quoted arg
    Received:
    Received:
    Received: