I have a script (let’s call it scriptC) that uses getopt to parse short and long options and works fine.

This script is being called like this:
scriptA runs scriptB which calls scriptC with the proper parameters.

Question: Is it possible to pass the same parameters as arguments to scriptA and then those be passed over eventually to scriptC?
The scripts are called like:
scriptB "$@" and in scriptB it eventually does scriptC —param1 —param2

  • 1
    getopt(1) is outdated since 1986, better use the standardized getopts(1). – schily May 22 '18 at 13:38
  • @schily:What do you mean standardized. E.g. I think Mac does not support it and I don't know if all Linux distros do – Jim May 23 '18 at 20:58
  • Apple definitely does - if not, tell them to withdraw their POSIX certification. Linux distros of course support it. Please note that getopt(1) has been made obsolete in 1986 because the interface definition makes it mainly unusable. getopts(1) is built into all shells (except csh) and maintains state variables as shell variables. – schily May 24 '18 at 9:14

If scriptA calls scriptB like

scriptB "$@"

then the command line arguments that were used for invoking scriptA will be passed to scriptB provided that these have not been altered before the call.

Likewise for the call from scriptB to scriptC.

As long as scriptA and scriptB does not try to interpret, change or otherwise mutate the contents of $@ (or the individual positional parameters $1, $2, $3 etc.), the command line arguments will be passed on to scriptC for it to parse with getopt.

Example using functions instead of scripts (it works the same way):


scriptC () {
    printf 'Arg: %s\n' "$@"

scriptB () {
    scriptC "$@"

scriptA () {
    scriptB "$@"

scriptA -param1 -param2

This will produce the output

Arg: -param1
Arg: -param2

Doing the call as

scriptA "hello world" --param1 /etc/passwd --param2

will produce

Arg: hello world
Arg: --param1
Arg: /etc/passwd
Arg: --param2

That is, the parameters will be passed on to scriptC without modification. It is then left to scriptC to interpret the parameters using getopt, getopts or by some other means.

  • what about --param1? – Jim May 22 '18 at 8:29
  • @Jim What about --param1? Anything you passed on the command line will be forwarded if you do it like this. --param, "hello world" or whatever was on the original command line. The point is that the intermediate scripts don't try to interpret what's on the command line, they just pass it on. – Kusalananda May 22 '18 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.