Skip to main content
Add more emphasis
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77
  • When storing -t 0.05 in a scalar shell variable and passing it as "$opt_string", the recipient will see this as one string containing a space (see above).
  • When storing -t and 0.05 in an array variable and passing it as "${opt_array[@]}" the recipient will see this as two separate items, the -t and the 0.05.(1)(2)
  • Many programs will use the getopt() function from the GNU C library for parsing command-line arguments, as is recommended by the POSIX guidelines.
  • The getopt() distinguishes "short" options and "long" option format, e.g. date -u or date --utc in case of the date command. The way option values for an option (say, -o / --option) are interpreted by getopt is usually -ovalue or -o value for short options and --option=value or --option value for long options.
  • When passing -t 0.05 as two words to a tool that uses getopt(), it will take the first character after the - as being the option name and the next word as the option value (the -o value syntax). So, read would take t as option name and 0.05 as option value.
  • When passing -t 0.05 as one word, it will be interpreted as the -ovalue syntax: getopt() will take (again) the first character after the - as the option name and the remainder of the string as option value, so the value would be 0.05 with a leading space with a leading space.
  • The read command apparently doesn't accept timeout specifications with a leading space. And indeed, if you call
    read -t " 0.05" -srn 1
    
    where the value is explicitly a string with leading space, read also complains about this.
  • When storing -t 0.05 in a scalar shell variable and passing it as "$opt_string", the recipient will see this as one string containing a space (see above).
  • When storing -t and 0.05 in an array variable and passing it as "${opt_array[@]}" the recipient will see this as two separate items, the -t and the 0.05.(1)(2)
  • Many programs will use the getopt() function from the GNU C library for parsing command-line arguments, as is recommended by the POSIX guidelines.
  • The getopt() distinguishes "short" options and "long" option format, e.g. date -u or date --utc in case of the date command. The way option values for an option (say, -o / --option) are interpreted by getopt is usually -ovalue or -o value for short options and --option=value or --option value for long options.
  • When passing -t 0.05 as two words to a tool that uses getopt(), it will take the first character after the - as being the option name and the next word as the option value (the -o value syntax). So, read would take t as option name and 0.05 as option value.
  • When passing -t 0.05 as one word, it will be interpreted as the -ovalue syntax: getopt() will take (again) the first character after the - as the option name and the remainder of the string as option value, so the value would be 0.05 with a leading space .
  • The read command apparently doesn't accept timeout specifications with a leading space. And indeed, if you call
    read -t " 0.05" -srn 1
    
    where the value is explicitly a string with leading space, read also complains about this.
  • When storing -t 0.05 in a scalar shell variable and passing it as "$opt_string", the recipient will see this as one string containing a space (see above).
  • When storing -t and 0.05 in an array variable and passing it as "${opt_array[@]}" the recipient will see this as two separate items, the -t and the 0.05.(1)(2)
  • Many programs will use the getopt() function from the GNU C library for parsing command-line arguments, as is recommended by the POSIX guidelines.
  • The getopt() distinguishes "short" options and "long" option format, e.g. date -u or date --utc in case of the date command. The way option values for an option (say, -o / --option) are interpreted by getopt is usually -ovalue or -o value for short options and --option=value or --option value for long options.
  • When passing -t 0.05 as two words to a tool that uses getopt(), it will take the first character after the - as being the option name and the next word as the option value (the -o value syntax). So, read would take t as option name and 0.05 as option value.
  • When passing -t 0.05 as one word, it will be interpreted as the -ovalue syntax: getopt() will take (again) the first character after the - as the option name and the remainder of the string as option value, so the value would be 0.05 with a leading space.
  • The read command apparently doesn't accept timeout specifications with a leading space. And indeed, if you call
    read -t " 0.05" -srn 1
    
    where the value is explicitly a string with leading space, read also complains about this.
Expansion and minor rephrasing of explanation
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

TheBut, first things first. In both of your examples, you place - as is recommended - quotes around the dereferencing of your shell variables, be it "${read_flags[@]}" in the array case or "$read_flags" in the scalar case. The main reason why it is recommended to always quote your shell variables is to prevent unwanted word splitting. Consider the following

  • When storing -t 0.05 in a scalar shell variable and passing it as "$opt_string", the recipient will see this as one string containing a space (see above).
  • When storing -t and 0.05 in an array variable and passing it as "${opt_array[@]}" the recipient will see this as two separate items, the -t and the 0.05.(1)(2)
  • Many programs will use the getopt() function from the GNU C library for parsing command-line arguments, as is recommended by the POSIX guidelines.
  • The getopt() distinguishes "short" options and "long" option format, e.g. date -u or date --utc in case of the date command. The way option values for an option (say, -o / --option) are interpreted by getopt is usually -ovalue or -o value for short options and --option=value or --option value for long options.
  • When passing -t 0.05 as two words to a tool that uses getopt(), it will take the first character after the - as being the option name and the next word as the option value (the -o value syntax). So, read would take t as option name and 0.05 as option value.
  • When passing -t 0.05 as one word, it will be interpreted as the -ovalue syntax: getopt() will take (again) the first character after the - as the option name and the remainder of the string as option value, so the value would be 0.05 with a leading space .
  • The read command apparently doesn't accept timeout specifications with a leading space. And indeed, if you call
    read -t " 0.05" -srn 1
    
    where the value is explicitly a string with leading space, read also complains about this.

(1) Note that using the @ (as opposed to *) makes a great difference here, because when the array reference is quoted, all array elements will then appear as if they were individually quoted and thus could contain spaces themselves without being split further.

(2) In principle, there is a third option: Store -t 0.05 in a scalar variable $opt_string, but pass it as $opt_string without the quotes. In this case, we would have word-splitting at the space, and again two items, -t and 0.05, would be passed separately to the program. However, this is not the recommended way because sometimes your argument value will have explicit whitespaces that need preserving.

The main reason why it is recommended to always quote your shell variables is to prevent unwanted word splitting. Consider the following

  • When storing -t 0.05 in a scalar shell variable and passing it as "$opt_string", the recipient will see this as one string containing a space (see above).
  • When storing -t and 0.05 in an array variable and passing it as "${opt_array[@]}" the recipient will see this as two separate items, the -t and the 0.05.(1)
  • Many programs will use the getopt() function from the GNU C library for parsing command-line arguments, as is recommended by the POSIX guidelines.
  • The getopt() distinguishes "short" options and "long" option format, e.g. date -u or date --utc in case of the date command. The way option values for an option (say, -o / --option) are interpreted by getopt is usually -ovalue or -o value for short options and --option=value or --option value for long options.
  • When passing -t 0.05 as two words to a tool that uses getopt(), it will take the first character after the - as being the option name and the next word as the option value (the -o value syntax). So, read would take t as option name and 0.05 as option value.
  • When passing -t 0.05 as one word, it will be interpreted as the -ovalue syntax: getopt() will take (again) the first character after the - as the option name and the remainder of the string as option value, so the value would be 0.05 with a leading space .
  • The read command apparently doesn't accept timeout specifications with a leading space. And indeed, if you call
    read -t " 0.05" -srn 1
    
    where the value is explicitly a string with leading space, read also complains about this.

(1) Note that using the @ (as opposed to *) makes a great difference here, because when the array reference is quoted, all array elements will then appear as if they were individually quoted and thus could contain spaces themselves without being split further.

But, first things first. In both of your examples, you place - as is recommended - quotes around the dereferencing of your shell variables, be it "${read_flags[@]}" in the array case or "$read_flags" in the scalar case. The main reason why it is recommended to always quote your shell variables is to prevent unwanted word splitting. Consider the following

  • When storing -t 0.05 in a scalar shell variable and passing it as "$opt_string", the recipient will see this as one string containing a space (see above).
  • When storing -t and 0.05 in an array variable and passing it as "${opt_array[@]}" the recipient will see this as two separate items, the -t and the 0.05.(1)(2)
  • Many programs will use the getopt() function from the GNU C library for parsing command-line arguments, as is recommended by the POSIX guidelines.
  • The getopt() distinguishes "short" options and "long" option format, e.g. date -u or date --utc in case of the date command. The way option values for an option (say, -o / --option) are interpreted by getopt is usually -ovalue or -o value for short options and --option=value or --option value for long options.
  • When passing -t 0.05 as two words to a tool that uses getopt(), it will take the first character after the - as being the option name and the next word as the option value (the -o value syntax). So, read would take t as option name and 0.05 as option value.
  • When passing -t 0.05 as one word, it will be interpreted as the -ovalue syntax: getopt() will take (again) the first character after the - as the option name and the remainder of the string as option value, so the value would be 0.05 with a leading space .
  • The read command apparently doesn't accept timeout specifications with a leading space. And indeed, if you call
    read -t " 0.05" -srn 1
    
    where the value is explicitly a string with leading space, read also complains about this.

(1) Note that using the @ (as opposed to *) makes a great difference here, because when the array reference is quoted, all array elements will then appear as if they were individually quoted and thus could contain spaces themselves without being split further.

(2) In principle, there is a third option: Store -t 0.05 in a scalar variable $opt_string, but pass it as $opt_string without the quotes. In this case, we would have word-splitting at the space, and again two items, -t and 0.05, would be passed separately to the program. However, this is not the recommended way because sometimes your argument value will have explicit whitespaces that need preserving.

Minor rephrasing
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77
  • When storing -t 0.05 in a scalar shell variable and passing it as "$opt_string", the recipient will see this as one string containing a space (see above).
  • When storing -t and 0.05 in an array variable and passing it as "${opt_array[@]}" the recipient will see this as two separate items, the -t and the 0.05.(1)
  • Many programs will use the getopt() function from the GNU C library for parsing command-line arguments, as is recommended by the POSIX guidelines.
  • The getopt() distinguishes "short" options and "long" option format, e.g. date -u or date --utc in case of the date command. The way option values for an option (say, -o / --option) are interpreted by getopt is usually -ovalue or -o value for short options and --option=value or --option value for long options.
  • When passing -t 0.05 as two words to a tool that uses getopt(), it will take the first character after the - as being the option name and the next word as the option value (the -o value syntax). So, read would take t as option name and 0.05 as option value.
  • When passing -t 0.05 as one word, it will be interpreted as the -ovalue syntax: getopt() will take (again) the first character after the - as the option name and the remainder of the string as option value, so the value would be 0.05 with a leading space  .
  • The read command apparently doesn't accept timeout specifications with a leading space. And indeed, if you call
    read -t " 0.05" -srn 1
    
    where the value is explicitly a string with leading space, read also complains about this.
  • When storing -t 0.05 in a scalar shell variable and passing it as "$opt_string", the recipient will see this as one string containing a space.
  • When storing -t and 0.05 in an array variable and passing it as "${opt_array[@]}" the recipient will see this as two separate items, the -t and the 0.05.(1)
  • Many programs will use the getopt() function from the GNU C library for parsing command-line arguments, as is recommended by the POSIX guidelines.
  • The getopt() distinguishes "short" options and "long" option format, e.g. date -u or date --utc in case of the date command. The way option values for an option (say, -o / --option) are interpreted by getopt is usually -ovalue or -o value for short options and --option=value or --option value for long options.
  • When passing -t 0.05 as two words to a tool that uses getopt(), it will take the first character after the - as being the option name and the next word as the option value. So, read would take t as option name and 0.05 as option value.
  • When passing -t 0.05 as one word, it will take (again) the first character after the - as the option name and the remainder of the string as option value, so the value would be 0.05 with a leading space.
  • The read command apparently doesn't accept timeout specifications with a leading space. And indeed, if you call
    read -t " 0.05" -srn 1
    
    where the value is explicitly a string with leading space, read also complains about this.
  • When storing -t 0.05 in a scalar shell variable and passing it as "$opt_string", the recipient will see this as one string containing a space (see above).
  • When storing -t and 0.05 in an array variable and passing it as "${opt_array[@]}" the recipient will see this as two separate items, the -t and the 0.05.(1)
  • Many programs will use the getopt() function from the GNU C library for parsing command-line arguments, as is recommended by the POSIX guidelines.
  • The getopt() distinguishes "short" options and "long" option format, e.g. date -u or date --utc in case of the date command. The way option values for an option (say, -o / --option) are interpreted by getopt is usually -ovalue or -o value for short options and --option=value or --option value for long options.
  • When passing -t 0.05 as two words to a tool that uses getopt(), it will take the first character after the - as being the option name and the next word as the option value (the -o value syntax). So, read would take t as option name and 0.05 as option value.
  • When passing -t 0.05 as one word, it will be interpreted as the -ovalue syntax: getopt() will take (again) the first character after the - as the option name and the remainder of the string as option value, so the value would be 0.05 with a leading space  .
  • The read command apparently doesn't accept timeout specifications with a leading space. And indeed, if you call
    read -t " 0.05" -srn 1
    
    where the value is explicitly a string with leading space, read also complains about this.
Expand on explanation, add reference to Q&A arount ${array[*]} and ${array[@]}
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77
Loading
Remaining typo
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77
Loading
added 2 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441
Loading
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77
Loading