Skip to main content
`foo=': ::'` with IFS=': ' would be split to 3 empty arguments.
Source Link
Stéphane Chazelas
  • 584.9k
  • 96
  • 1.1k
  • 1.7k

result is true. But I don't understand why since bash will translate $anything to an empty string, shouldn't this will result in syntax error ?

Close, but no biscuit. There is an expression: $anything. if ; then is an error because bash fails to parse it (hence: syntax error). It expects a command list there and gets a ; instead. Parsing happens well before variable expansion happens, so if $anything; then parses fine. What happens next? $anything is expanded, and field splitting, etc. done. This leaves an empty command list, which is trivially true. Compare:

if "$anything"; then echo foo; fi
if $anything; then echo foo; fi

"$anything"; isn't an empty command list, it has command which happens to an empty string, the same as with if '';. But an unquoted "$anything" expands to nothing. 

The same would be true if $anything contained only whitespace characters present in IFS, or contained a $IFS-separated list of globs that didn't match any file and the nullglob option was on.

result is true. But I don't understand why since bash will translate $anything to an empty string, shouldn't this will result in syntax error ?

Close, but no biscuit. There is an expression: $anything. if ; then is an error because bash fails to parse it (hence: syntax error). It expects a command list there and gets a ; instead. Parsing happens well before variable expansion happens, so if $anything; then parses fine. What happens next? $anything is expanded, and field splitting, etc. done. This leaves an empty command list, which is trivially true. Compare:

if "$anything"; then echo foo; fi
if $anything; then echo foo; fi

"$anything"; isn't an empty command list, it has command which happens to an empty string, the same as with if '';. But an unquoted "$anything" expands to nothing. The same would be true if $anything contained only characters present in IFS.

result is true. But I don't understand why since bash will translate $anything to an empty string, shouldn't this will result in syntax error ?

Close, but no biscuit. There is an expression: $anything. if ; then is an error because bash fails to parse it (hence: syntax error). It expects a command list there and gets a ; instead. Parsing happens well before variable expansion happens, so if $anything; then parses fine. What happens next? $anything is expanded, and field splitting, etc. done. This leaves an empty command list, which is trivially true. Compare:

if "$anything"; then echo foo; fi
if $anything; then echo foo; fi

"$anything"; isn't an empty command list, it has command which happens to an empty string, the same as with if '';. But an unquoted "$anything" expands to nothing. 

The same would be true if $anything contained only whitespace characters present in IFS, or contained a $IFS-separated list of globs that didn't match any file and the nullglob option was on.

Source Link
muru
  • 78k
  • 16
  • 212
  • 318

result is true. But I don't understand why since bash will translate $anything to an empty string, shouldn't this will result in syntax error ?

Close, but no biscuit. There is an expression: $anything. if ; then is an error because bash fails to parse it (hence: syntax error). It expects a command list there and gets a ; instead. Parsing happens well before variable expansion happens, so if $anything; then parses fine. What happens next? $anything is expanded, and field splitting, etc. done. This leaves an empty command list, which is trivially true. Compare:

if "$anything"; then echo foo; fi
if $anything; then echo foo; fi

"$anything"; isn't an empty command list, it has command which happens to an empty string, the same as with if '';. But an unquoted "$anything" expands to nothing. The same would be true if $anything contained only characters present in IFS.