From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: TJ Luoma <luomat@gmail.com>
Cc: Zsh MailingList <zsh-users@zsh.org>
Subject: Re: Expanding a variable extracted from another file
Date: Sat, 16 May 2020 21:27:55 +0000 [thread overview]
Message-ID: <20200516212755.28d7cd1e@tarpaulin.shahaf.local2> (raw)
In-Reply-To: <CADjGqHt8oF0W9AEU+j+yJHfOxxrVh-6HFP8ediAT0A_+qX9N8A@mail.gmail.com>
TJ Luoma wrote on Sat, 16 May 2020 02:46 -0400:
> Even if I do:
>
> echo "$MYVAR"
>
> I get the literal "$HOME" including double-quotes in the output.
>
> I guess this must be some foundational Unix thing that I don't
> understand properly, but all of my attempts to google it have come up
> short because I'm not sure what to search for other than "expand
> variable" but it's not quite that either.
Variable values can be any string of bytes. If you can create
a file that contains something, you can set a variable's value to that
something.
When you type «echo "$HOME"» on the command line, HOME is the name of
a variable whose value is «/home/alice». However, it's also possible to
have a variable whose value is literally «"$HOME"» (7 bytes). If you
print that variable's value, you'll get those 7 bytes back verbatim:
.
% s='"$HOME"'
% echo $s
"$HOME"
%
Values don't undergo variable expansion. If they did, stuff like
.
% s='"$s"'
% echo $s
.
would either crash or hang (depending on how precisely it's implemented).
Flags such as ${(e)foo}, ${~foo}, and ${(P)foo} let you specifically ask for
a word to be expanded. Even then, only one level of expansion happens:
.
% s='$foo'
% foo='$bar'
% bar='This is bar'
% print -r -- ${s}
$foo
% print -r -- ${(e)s}
$bar
%
Whether any of these flags is the right tool for the job depends on the context.
Cheers,
Daniel
P.S. «echo $s» does _not_ print $s verbatim for arbitrary values of $s —
for example, backslash escape sequences would be expanded — but that
doesn't matter in this case.
prev parent reply other threads:[~2020-05-16 21:28 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-16 6:46 TJ Luoma
2020-05-16 7:03 ` Bart Schaefer
2020-05-16 7:13 ` Roman Perepelitsa
2020-05-16 16:58 ` Bart Schaefer
2020-05-17 12:10 ` TJ Luoma
2020-05-16 21:27 ` Daniel Shahaf [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200516212755.28d7cd1e@tarpaulin.shahaf.local2 \
--to=d.s@daniel.shahaf.name \
--cc=luomat@gmail.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).