Bash expansion

I had posted about the weird behavior of bash expansion, but I think I just ran across a wiki page that discusses this issue.

The key issue seems to be the order of expansions:

  1. Syntax analysis (Parsing)
  2. Brace expansion
  3. Tilde expansion
  4. Parameter and variable expansion
  5. Command substitution
  6. Arithmetic expansion
  7. Word splitting
  8. Filename expansion
  9. Quote removal

Command substitution happens after brace expansion, but before filename expansion (which * expansion is). Similarly, if you attempt to do tilde expansion, it also “won’t work”:

maciek@anemone:~$ echo $(echo \~)

Mystery solved. I ran across this when trying to figure out what was it that eval did that’s more interesting than spawning a new subshell. Clearly, the answer is that it lets you get a second pass through these steps, which can definitely be handy.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: