On Thu, Dec 19, 2019 at 08:51:51AM -0500, Greg Wooledge wrote:
> On Thu, Dec 19, 2019 at 10:03:57AM +0200, Andrei POPESCU wrote:
> > On Mi, 18 dec 19, 21:42:21, rhkramer@gmail.com wrote:
> > > On Wednesday, December 18, 2019 12:26:04 PM tomas@tuxteam.de wrote:
> > > > #!/bin/bash
> > > > home=${HOME:-~}
> >
> > It will set the variable 'home' to the value of the variable 'HOME' if
> > set (yes, case matters), otherwise to '~'.
>
> It appears to expand the ~, rather than assigning a literal ~ character
> to the variable.
For bash, it's in the docs:
Quoth the man page:
${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion
of word is substituted. Otherwise, the value of parameter is
substituted.
For the rest...
I agree that the shell is full of bashisms. I usually don't care very
much when it's a script "to use around home". Whenever scripts get
larger or more widely distributed, I put in some effort.
But thanks for your (as always) insightful comments!
[...]
> So, home=${HOME:-~} seems like some sort of belt-and-suspenders fallback
> check in case the script is executed in a context where $HOME hasn't been
> set. Maybe in a systemd service or something similar? That's all I
> can think of.
You are right: HOME belongs to the blessed shell variables (in bash, at
least). Moreover, tilde expansion is done, according to the docs, using
HOME.
Quoth (again) the man page:
HOME The home directory of the current user; the default argument
for the cd builtin command. The value of this variable is
also used when performing tilde expansion.
In practical terms:
tomas@trotzki:~$ export HOME=rumpelstilzchen
tomas@trotzki:/home/tomas$ echo ~
rumpelstilzchen
:-)
So this whole "fallback to tilde thing is redundant (at least in bash)!
Cheers
-- tomás
Attachment:
signature.asc
Description: Digital signature