I have the following commands in my script:set -- `getopt -q agvc:l:t:i: $@` ... while [ -n $1 ] do -i) TIME_GAP_BOOT=$2 shift ;; ... sleep $TIME_GAP_BOOTWhen invoking the script with -i 2, I get the error sleep: invalid time interval `\'2\''What am I doing wrong? How do I correctly format the argument?
How do I fix the argument received from getopt?
bash;bash script;getopts
The bash builtin getopts is a lot easier to use. If you're using bash, you should use it instead of getopt.GNU getopt is designed to work with arguments which have whitespace and other metacharacters in them. In order to do that, it produces a result string with bash-style quotes (or csh-style quotes, depending on the -s option.) You need to arrange to have the quotes interpreted, which requires the use of eval. (Did I mention that the bash builtin getopts is better?).The following example is from the getopt distribution; I had nothing to do with it. (It should be present on your machine somewhere; with ubuntu and debian, it shows up as /usr/share/doc/util-linux/examples/getopt-parse.bash. I'm only quoting a few lines:# Note that we use `$@' to let each command-line parameter expand to a # separate word. The quotes around `$@' are essential!# We need TEMP as the `eval set --' would nuke the return value of getopt.TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \     -n 'example.bash' -- $@`if [ $? != 0 ] ; then echo Terminating... >&2 ; exit 1 ; fi# Note the quotes around `$TEMP': they are essential!eval set -- $TEMPIn addition to the quotes which the example's comment points at, it's important to look at the eval, which is generally frowned upon.By contrast, the bash builtin getopts requires no eval, and is quite straightforward; it basically emulates the standard C library call:while getopts agvc:l:t:i: opt; do  case $opt in   i) TIME_GAP_BOOT=$OPTARG;;   # ...  esacdone