title: How can I pass options to another script in bash only if they have been set?
body: <p>I have two bash scripts, <code>do-foo-for-thing.sh</code> and <code>do-foo-for-many-things.sh</code>.</p>
  <p><code>do-foo-for-thing.sh</code> takes a number of arguments, parsed using getopts, most of which are optional and have default values defined in the script.</p>
  <p>I would like <code>do-for-for-many-things.sh</code> to call <code>do-for-for-thing.sh</code> with any of these options that have been provided, but not to use any options that have not been provided (so that they are not required for the -many-things script, and they single-thing scripts owns the default values).</p>
  <p>I've tried to look up ways to do this, but had no luck - I'm not super familiar with bash. I can easily pass in all arguments, but that doesn't work if I don't want to provide all the potential options to the -many-things script.</p>
  <p>Rough examples below;</p>
  <p>do-foo-for-thing.sh</p>
  <pre><code>A=default-A
  B=default-B
  C=default-C
  
  if [ $# -ge 1 ]; then
    OPTIND=1
    while getopts &quot;a:b:c:p:&quot; opt; do
      case &quot;$opt&quot; in
        a) A=$OPTARG ;;
        B) B=$OPTARG ;;
        C) C=$OPTARG ;;
        P) P=$OPTARG ;;
        \?) echo &quot;Unrecognized option: -$OPTARG&quot; &gt;&amp;2; exit 1 ;;
      esac
    done
    shift $((OPTIND-1))
  fi
  
  
  do-foo-that-uses-values-A-B-and-C-on-P
  </code></pre>
  <p>do-foo-for-many-things.sh</p>
  <pre><code>if [ $# -ge 1 ]; then
    OPTIND=1
    while getopts &quot;d:a:b:c:&quot; opt; do
      case &quot;$opt&quot; in
        d) D=$OPTARG ;;
        a) A=$OPTARG ;;
        B) B=$OPTARG ;;
        C) C=$OPTARG ;;
        \?) echo &quot;Unrecognized option: -$OPTARG&quot; &gt;&amp;2; exit 1 ;;
      esac
    done
    shift $((OPTIND-1))
  fi
  
  for P in `ls D`
  do
    ./do-for-for-thing.sh -p P
  done
  </code></pre>
  <p>This currently only passes P in. I'd like to pass in A, B, and C as well - but only if they've been provided to this script (so that any that have not been use the default values in do-foo-for-thing).</p>