* string starts with ...
@ 2006-01-21 13:53 Matthias Berndt
2006-01-21 14:05 ` Hannu Koivisto
0 siblings, 1 reply; 4+ messages in thread
From: Matthias Berndt @ 2006-01-21 13:53 UTC (permalink / raw)
To: zsh-users
Greetings,
how can I test if a string starts with '#'?
I can't find the trick.
Thanks
Matthias
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: string starts with ...
2006-01-21 13:53 string starts with Matthias Berndt
@ 2006-01-21 14:05 ` Hannu Koivisto
2006-01-21 17:02 ` Bart Schaefer
0 siblings, 1 reply; 4+ messages in thread
From: Hannu Koivisto @ 2006-01-21 14:05 UTC (permalink / raw)
To: Matthias Berndt; +Cc: zsh-users
Matthias Berndt <berndt.matthias@gmx.de> writes:
> how can I test if a string starts with '#'?
> I can't find the trick.
[[ $YOURSTRING == \#* ]]
See "Conditional Expressions" in the manual.
--
Hannu
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: string starts with ...
2006-01-21 14:05 ` Hannu Koivisto
@ 2006-01-21 17:02 ` Bart Schaefer
2006-01-23 10:45 ` Peter Stephenson
0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2006-01-21 17:02 UTC (permalink / raw)
To: zsh-users
On Jan 21, 4:05pm, Hannu Koivisto wrote:
} Subject: Re: string starts with ...
}
} Matthias Berndt <berndt.matthias@gmx.de> writes:
}
} > how can I test if a string starts with '#'?
}
} [[ $YOURSTRING == \#* ]]
}
} See "Conditional Expressions" in the manual.
I think the problem isn't understanding of conditional expressions, it's
that '#' normally starts a comment, so you have to quote it.
} > I can't find the trick.
The "trick" is to quote ONLY the '#' character and not the rest of the
pattern. Hence Hannu's solution is the most concise, but these work:
[[ $YOURSTRING == '#'* ]]
[[ $YOURSTRING == "#"* ]]
String concatenation in shell language is by simple juxtaposition, no
operator required.
This works, too, by forcing the '#' to be treated as part of a pattern:
[[ $YOURSTRING == (#)* ]]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: string starts with ...
2006-01-21 17:02 ` Bart Schaefer
@ 2006-01-23 10:45 ` Peter Stephenson
0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2006-01-23 10:45 UTC (permalink / raw)
To: zsh-users
Bart Schaefer wrote:
> This works, too, by forcing the '#' to be treated as part of a pattern:
>
> [[ $YOURSTRING == (#)* ]]
You mean with extendedglob turned off.
I tried it with extendedglob turned on (this is for aficionados only):
% [[ 'foo' = (#)foo ]] && echo yes
yes
It looks like it's valid to have zero globbing flags. The manual
doesn't actually say this in as many words, but it's sort of useful:
% typeset approx
% [[ foo = (#$approx)fo ]] && echo yes
% approx=a1
% [[ foo = (#$approx)fo ]] && echo yes
yes
Note no globsubst trickery needed: $approx can be a straight string as
long as the # is unquoted. This probably needs documenting. However,
I think all globbing flags can be turned off by other flags, so having
none isn't actually that useful.
--
Peter Stephenson <pws@csr.com> Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK Tel: +44 (0)1223 692070
To access the latest news from CSR copy this link into a web browser: http://www.csr.com/email_sig.html
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2006-01-23 10:52 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-01-21 13:53 string starts with Matthias Berndt
2006-01-21 14:05 ` Hannu Koivisto
2006-01-21 17:02 ` Bart Schaefer
2006-01-23 10:45 ` Peter Stephenson
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).