Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: egrep --color=always 'zsh\.'
On Tue, Apr 2, 2024, at 4:53 PM, Ray Andrews wrote:
> On 2024-04-02 13:14, Lawrence Velázquez wrote:
>> The issue is probably that you are using the same pattern for both "apt-file search" and egrep, but they don't interpret it in the
>> same way. By default, "apt-file search" does a literal substring
>> match, so "\." will match backslash-period.
> Right, I know that, my code sample was too lazy. The only issue is
> with the egrep.
The solution is to stop using egrep.
>> The function as presented cannot actually work with regex arguments,
>> so just switch to "grep -F" and avoid the escaping issues altogether. If there's no way for the shell to handle it, then that's just what I'll do.
If you're going to continue using "apt-file search $1" then it
simply does not make sense for $1 to be a regex, and it follows
that it does not make sense to use "egrep $1". Using "grep -F"
removes the need to try slicing and dicing the argument at all.
Don't use the shell to paper over bad design when you can just
fix the design.
> Still .... I'm almost sure I've had to deal with this before. IIRC every situation I've come up against has been handled by the right combination of quotes and backslashes ... I think.
It is easy to escape characters that are special to zsh itself.
It is not so easy to escape characters that are special to POSIX
extended regex. (The rules are hardly impossible to work out,
but they're not straightforward either.)
>> No, egrep does not need the quotes.
>> % apt-file search zsh-theme | egrep --color=always zsh.
>> ...
>> zsh-theme-powerlevel9k: /usr/share/powerlevel9k/powerlevel9k.zsh-theme
>>
>> % apt-file search zsh-theme | egrep --color=always zsh\.
>> ...
>> zsh-theme-powerlevel9k: /usr/share/powerlevel9k/powerlevel9k.zsh-theme
>>
>> % apt-file search zsh-theme | egrep --color=always 'zsh.'
>> ...
>> zsh-theme-powerlevel9k: /usr/share/powerlevel9k/powerlevel9k.zsh-theme
These three commands are effectively identical; they invoke egrep
with the pattern "zsh.".
>> % apt-file search zsh-theme | egrep --color=always 'zsh\.'
>> [ no match ]
>
> ... the dot is matching the dash -- or anything else -- unless I use: 'zsh\.'
Yes, you have to quote properly to ensure that egrep receives the
necessary "zsh\." argument -- just as with any other command. But
egrep itself does not "need" the quotes; it does not see them at
all. This command is uglier but works just as well:
% apt-file search zsh-theme | egrep --color=always zsh\\.
"Preserving" the quotes, as you considered in your first message,
would in fact break the command.
--
vq
Messages sorted by:
Reverse Date,
Date,
Thread,
Author