On Mon, Oct 27, 2025 at 04:08:35PM -0400, Chet Ramey wrote: > On 10/25/25 12:52 PM, Maksym Telychko wrote: > > > Bash Version: 5.2 > > Patch Level: 37 > > Release Status: release > > > > Description: > > The backslash escape symbol works inconsistently when unquoted > > parameter expansion occurs > > The problem, if it can be termed one, is that backslash is special to both > the shell's token recognition and word expansion, and to pathname > expansion. Bash doesn't really attempt to do anything special with unquoted > parameter expansions -- the results are simply passed to the globbing > functions (or not, if there are no unquoted globbing characters). > > If the parameter expansion is quoted, bash does its best to prevent the > characters that are special to pathname expansion from being treated > specially. > > > Repeat-By: > > > > Say, I have a directory named '\*123' (literally: backslash, asterisk, > > one, two, three). > > For being more clean, set the shell option `shopt -s nullglob` > > > > Following code produces unexpected results: > > > > pat='\*' > > echo $pat > > It prints '\*' instead of '\*123'. > > Virtually all shells do this (mksh is an outlier here), but they may get > there different ways. Bash does it this way: > > > Looks like backslash works as escape symbol here, but for asterisk only. > > The backslash prevents the `*' from being recognized as a pathname > expansion character, so the shell never attempts pathname expansion. > > Other shells may pass the string to pathname expansion, but the backslash > will prevent the `*' from having its special meaning.>If you assign > `pat='\\'`, it will print '\\' as expected. > > Yes. Backslash isn't one of the characters that's special to > pathname expansion, so this isn't really relevant. > > > To get expected result,> you need to assign `pat='\\*'`. I think it's > > confusing. > > Since you're using an unquoted word expansion, you have to prevent the > backslash from quoting the `*'. A backslash quoting the backslash is one > way to do that. > > -- > ``The lyf so short, the craft so long to lerne.'' - Chaucer > ``Ars longa, vita brevis'' - Hippocrates > Chet Ramey, UTech, CWRU [email protected] http://tiswww.cwru.edu/~chet/
Thank you! I'm appreciated for the detailed explanation. -- Maksym Telychko
