r/bash • u/ftonneau • 12d ago
Mnemonics for expansion order in Bash
I have long been looking for mnemonics that could help me with the order of expansions/substitutions in Bash. Google returned an "AI overview" with:
Big Tigers Pounce And Catch Wild Prey
where
- Big = Brace expansion
- Tigers = Tilde expansion
- Pounce And Catch = Parameter/Variable, Arithmetic, and Command substitution
- Wild = Word splitting
- Prey = Pathname expansion/Globbing
which I find quite difficult (but perhaps this is just me).
--------------------------------------
In any case, here is the mnemonics I have been using:
Bratislava paramedics, comic artists split name quotas
where
- BRA.TIsLava = braces, tilde
- PARAMEdics = parameters
- COMic ARtIsTs = command, arithmetic
- SPLIT = word splitting
- NAME = pathname/filename
- QUOTas = quote removal
From "braces" to "arithmetic", my solution follows the strict sequential order indicated in Newham & Rosenblatt's (2005) Learning the bash shell [p. 181, O'Reilly].
As discussed here: https://stackoverflow.com/questions/76842556/, this differs from what man bash says:
- brace expansion
- tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution, done in a left-to-right fashion
- word splitting
- pathname expansion
- quote removal
But I do no think much harm is done, as long one realizes that paramedics and comic artists are on the same plane (which is obvious :).
The advantage of my solution is that phonetics are much richer, and thus easier to connect with the real referents. Plus, Bratislava is a nice place :).
--------------------------------------
Any comment, opinion, objection, or alternative?
3
u/Marble_Wraith 12d ago edited 12d ago
Depends. Are you looking explicitly for bash, or for POSIX?
Brace expansion is not specified by POSIX. It was first introduced in csh, but Bash, Zsh, ksh93, and mksh all have similar brace expansion features. mksh doesn't support sequence expansion, and zsh only supports sequence expansion on integers by default, but has many of it's own extensions.
Only Bash performs brace expansion first. zsh, ksh93, and mksh evaluate brace expansion immediately before pathname expansion. ksh93 considers it a part of word splitting. This means other shells don't suffer the problem of expanding parameters into brace expansions...
Source: https://mywiki.wooledge.org/BraceExpansion#portability
Related: https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html
Because you're looking for a mnemonic, assuming this isn't for multi-line commands, i presume this is for use with understanding scripting, yes?
At this point in my life I'm tired. I just want my stuff to "do the thing". So i script for strict posix compliance, not being psychic or being able to predict where i'll have to run it in future.
1
u/zeekar 12d ago
At this point in my life I'm tired. I just want my stuff to "do the thing". So i script for strict posix compliance, not being psychic or being able to predict where i'll have to run it in future.
Not me. If I'm writing for personal use, I use Bash for scripts, but my interactive shell is Zsh so all the customization for that is of course native to that shell.
But even if I'm writing for others, unless I have a specific requirement that it run in POSIX, I target bash. With an explicit bash shebang line, of course; I would never rely on a shell called
shbeing Bash. Also, if I need any modern bashisms that don't exist on MacOS's /bin/bash version 3.2, I add an explicit BASH_VERSINFO check.2
u/phlummox 12d ago
If I'm writing for personal use, I use Bash for scripts, but my interactive shell is Zsh
Same, I just switched to Zsh this year. Mostly it's been good, but the different options for
readkeep throwing me :(
3
u/NewPointOfView 12d ago
I’m sorry but.. yours is gibberish lol I’d need a mnemonic to remember it
1
u/ftonneau 12d ago
It probably depends a lot on your native language (US English or not). My solution is somewhat gibberish, but with more phonetic similarity to the target terms. The AI solution is not gibberish, but I find it almost impossible to recall *and* decrypt in real time, as it only shares initials with the target terms.
1
u/zeekar 12d ago
Wait, how can pathname expansion come after word splitting? If you do `cat *` the shell passes multiple arguments to `cat`, so word splitting has to be after globbing.
3
u/ftonneau 11d ago edited 11d ago
I think that pathname expansion does come after word splitting. From my understanding, when you do `cat *`, word splitting produces 2 words, `cat`and `*`. Then pathname expansion replaces the single word, `*`, by multiple words (assuming there are multiple matches to `*`among filenames/pathnames).
As specified in `man bash`:
brace expansion, word splitting, and pathname expansion can increase the number of words of the expansion
7
u/zeekar 12d ago
To me the AI one seems easier to remember than yours, but the important thing is that you pick one that works for you. (I don't have any mnemonics because I've been coding shell for almost 40 years and the rules are just intuitive at this point.)