* variable expansion inside string
@ 2019-07-30 12:28 ` Pier Paolo Grassi
2019-07-30 13:18 ` Peter Stephenson
0 siblings, 1 reply; 6+ messages in thread
From: Pier Paolo Grassi @ 2019-07-30 12:28 UTC (permalink / raw)
To: Zsh-Users List
[-- Attachment #1: Type: text/plain, Size: 499 bytes --]
Hello, is it possibile to do only variable expansion inside a string, eg:
file="filename.txt"
a="*$file*"
I would like to print $a as:
*filename.txt*
I have tried ${(P)a}
and
eval echo -n - $a
but neither do what I would like, because $a does not contains only a
variable name, and the * are expanded by glob subst in the eval case.
thanks in advance
Pier Paolo Grassi
linkedin: https://www.linkedin.com/in/pier-paolo-grassi-19300217
founder: https://www.meetup.com/it-IT/Machine-Learning-TO
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: variable expansion inside string
2019-07-30 12:28 ` variable expansion inside string Pier Paolo Grassi
@ 2019-07-30 13:18 ` Peter Stephenson
2019-07-30 14:02 ` Pier Paolo Grassi
0 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2019-07-30 13:18 UTC (permalink / raw)
To: zsh-users
On Tue, 2019-07-30 at 14:28 +0200, Pier Paolo Grassi wrote:
> Hello, is it possibile to do only variable expansion inside a string, eg:
>
> file="filename.txt"
> a="*$file*"
If you run zsh -f, you'll see that that's exactly what you normally get, just
by running
print "$a"
--- no eval.
My best guess is you have the option globsubst set and when you tried
to print the result, you didn't use double quotes at that point, i.e.
you had something like
print $a
and the *filename.text* got expanded at that point.
So the original code is actually fine.
pws
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: variable expansion inside string
2019-07-30 13:18 ` Peter Stephenson
@ 2019-07-30 14:02 ` Pier Paolo Grassi
2019-07-30 14:15 ` Peter Stephenson
0 siblings, 1 reply; 6+ messages in thread
From: Pier Paolo Grassi @ 2019-07-30 14:02 UTC (permalink / raw)
To: Peter Stephenson; +Cc: Zsh-Users List
[-- Attachment #1: Type: text/plain, Size: 1406 bytes --]
thank you Peter, I made a mistake in the example code:
file="filename.txt"
a="*$file*"
should have been:
file="filename.txt"
a='*$file*'
with single quotes the behaviour is changed:
zsh -fc 'file= filename.txt; a="*$file*"; print "$a"'
gives:
*filename.txt*
instead with single quotes:
zsh -fc 'file= filename.txt; a=''*$file*''; print "$a"'
gives
*$file*
I would like the second example to somehow give the result of the first,
since I am trying to pass the string from another context where the
variable $file is not already defined
thanks
Pier Paolo Grassi
linkedin: https://www.linkedin.com/in/pier-paolo-grassi-19300217
founder: https://www.meetup.com/it-IT/Machine-Learning-TO
Il giorno mar 30 lug 2019 alle ore 15:19 Peter Stephenson <
p.stephenson@samsung.com> ha scritto:
> On Tue, 2019-07-30 at 14:28 +0200, Pier Paolo Grassi wrote:
> > Hello, is it possibile to do only variable expansion inside a string, eg:
> >
> > file="filename.txt"
> > a="*$file*"
>
> If you run zsh -f, you'll see that that's exactly what you normally get,
> just
> by running
>
> print "$a"
>
> --- no eval.
>
> My best guess is you have the option globsubst set and when you tried
> to print the result, you didn't use double quotes at that point, i.e.
> you had something like
>
> print $a
>
> and the *filename.text* got expanded at that point.
>
> So the original code is actually fine.
>
> pws
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: variable expansion inside string
2019-07-30 14:02 ` Pier Paolo Grassi
@ 2019-07-30 14:15 ` Peter Stephenson
2019-07-30 14:23 ` Peter Stephenson
0 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2019-07-30 14:15 UTC (permalink / raw)
To: Zsh-Users List
So really what you're trying to do is: given a variable "a" containing
exactly the literal characters
*$file*
(however they got there) expand any variable references, but nothing
else.
The nearest I can think of is:
print -r -- "${(e):-$a}"
That won't be limited to just variables, it'll do other sorts
of expansion, but it won't do globbing, and you don't have the
hairiness of an "eval".
pws
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: variable expansion inside string
2019-07-30 14:15 ` Peter Stephenson
@ 2019-07-30 14:23 ` Peter Stephenson
2019-07-30 14:55 ` Pier Paolo Grassi
0 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2019-07-30 14:23 UTC (permalink / raw)
To: Zsh-Users List
On Tue, 2019-07-30 at 15:15 +0100, Peter Stephenson wrote:
> The nearest I can think of is:
>
> print -r -- "${(e):-$a}"
Or, actually, a straight
print -r -- "${(e)a}"
since you don't need to add any text to the variable substitution.
pws
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: variable expansion inside string
2019-07-30 14:23 ` Peter Stephenson
@ 2019-07-30 14:55 ` Pier Paolo Grassi
0 siblings, 0 replies; 6+ messages in thread
From: Pier Paolo Grassi @ 2019-07-30 14:55 UTC (permalink / raw)
To: Peter Stephenson; +Cc: Zsh-Users List
[-- Attachment #1: Type: text/plain, Size: 751 bytes --]
thanks Peter, for my use case it actually works really nice this way. I was
hoping for something that wouldn't allow arbitrary command execution for
future uses, but I guess that would need a proper templating system of sorts
best regards
Pier Paolo Grassi
linkedin: https://www.linkedin.com/in/pier-paolo-grassi-19300217
founder: https://www.meetup.com/it-IT/Machine-Learning-TO
Il giorno mar 30 lug 2019 alle ore 16:24 Peter Stephenson <
p.stephenson@samsung.com> ha scritto:
> On Tue, 2019-07-30 at 15:15 +0100, Peter Stephenson wrote:
> > The nearest I can think of is:
> >
> > print -r -- "${(e):-$a}"
>
> Or, actually, a straight
>
> print -r -- "${(e)a}"
>
> since you don't need to add any text to the variable substitution.
>
> pws
>
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2019-07-30 14:56 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <CGME20190730123021epcas1p21671587b575bd82a2512bcb1fa091563@epcas1p2.samsung.com>
2019-07-30 12:28 ` variable expansion inside string Pier Paolo Grassi
2019-07-30 13:18 ` Peter Stephenson
2019-07-30 14:02 ` Pier Paolo Grassi
2019-07-30 14:15 ` Peter Stephenson
2019-07-30 14:23 ` Peter Stephenson
2019-07-30 14:55 ` Pier Paolo Grassi
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).