0

When does history expansion happen?

  1. From bash manual

    Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’, and, when history expansion is enabled, ‘!’.

    Since double quotes are recognized at parsing stage by the parser, is it correct that history expansion must happen after parsing?

    If yes, when does it happen with respect to shell expansions such as brace expansion, parameter expansion, filename expansion, etc?

  2. But I think that history expansion is provided by the readline of the shell, so is processed before lexical analysis and parsing? Just like auto-completion in shell. Am I missing something?

Thanks.

6

Quoting the bash manual:

History expansion is performed immediately after a complete line is read, before the shell breaks it into words.

History expansion is the first stage of processing, even before shell parsing, which is why double quotes don’t protect !: the latter is processed before double quotes. It is handled by the history library, which implements its own parsing, with a few ways of protecting the history operator:

Only ‘\’ and ‘'’ may be used to escape the history expansion character, but the history expansion character is also treated as quoted if it immediately precedes the closing double quote in a double-quoted string.

By the time the shell’s parser starts handling a string, it’s already been parsed by the history library and history expansion has already taken place.

  • "... but the history expansion character is also treated as quoted if it immediately precedes the closing double quote in a double-quoted string". This doesn't appear to be the case from experience and is in direct contradiction to Bash Reference Manual: Double Quotes. – dev Oct 2 '18 at 15:01
  • @dev, that’s a quote from the bash manual (see the link). Try echo "hello!" in bash to see what’s meant here; that works without trying to expand history. – Stephen Kitt Oct 2 '18 at 15:11
  • I'm well aware this is a quote from the manual, but for me echo "hello!" performed history expansion as usual. Your comment made me check my bash version , and guess what, I was on 3.2 (using zsh as my main shell, so bash doesn't get much love these days). Upgraded to 4.4 and we're finally on the same page. It should be noted though, that even though the manual on Double Quotes does go into the specifics of escaping !, it fails to mention the "precedes the closing double quote" case. – dev Oct 3 '18 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.