1

When passing arguments to a Bash-script like:

1   while getopts u:d:p:f: option 
2   do 
3   case "${option}" in 
4   u) USER=${OPTARG};; 
5   d) DATE=${OPTARG};; 
6   p) PRODUCT=${OPTARG};; 
7   f) FORMAT=$OPTARG;; 
8   esac 
9   done

from: http://linux.about.com/od/Bash_Scripting_Solutions/a/How-To-Pass-Arguments-To-A-Bash-Script.htm

What is the difference between ${OPTARG};; line 4-5-6 and $OPTARG;; line 7?

RafaSashi
  • 16,483
  • 8
  • 84
  • 94
  • 4
    Nothing. They are identical. You need the `${var}` form when you need to expand a variable *inside* a word. `echo foo${var}bar` as opposed to `echo foo$varbar` (which will be seen as the `$foobar` variable). – Etan Reisner Apr 03 '15 at 18:11
  • thanks Etan! and is `echo foo${var}bar` equivalent to `echo "foo"$var"bar"` ? – RafaSashi Apr 03 '15 at 18:22
  • @RafaSashi `echo "foo"$var"bar"` won't work it's not properly quoted, `echo foo${var}bar` is equivalent to `echo "foo$var"bar` – Tiago Lopo Apr 03 '15 at 18:41
  • `echo "foo"$var"bar"` will work (as `$var`) and is syntactically validly quoted. It is also exactly backwards from a quoting perspective as it quotes the literal strings and not the variable expansion. – Etan Reisner Apr 03 '15 at 18:49

0 Answers0