* Setting GLOB_DOTS for a single command
@ 2021-11-21 4:46 Zach Riggle
2021-11-21 5:11 ` FW: " agkozak
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Zach Riggle @ 2021-11-21 4:46 UTC (permalink / raw)
To: Zsh Users
[-- Attachment #1: Type: text/plain, Size: 2910 bytes --]
Hello all!
I recently learned about GLOB_DOTS, which is pretty useful in some
scenarios, e.g.
echo *(md-1)
To show all files modified within the last day. However, in some cases I
want to see hidden files (e.g. ./.foo) as well. The globdots option is
excellent for this!
Is there an easy way to set this for a single expression?
I know I can use a function / anonymous function combined
with LOCAL_OPTIONS to get this, but I wondered if there's something more
clever.
show() {
setopt | grep glob
}
echo ----- Before -----
show
(){
setopt globdots
echo ----- Inside -----
show
setopt localoptions
}
echo ----- After -----
show
I expect that there's something I can do for scripts that I fully control,
to declare some function ('globdots') which is invoked with noglob, and can
then internally set the flag, and then trigger expansion of each argument
in "$@".
globdots() {
setopt globdots
local arguments=( $@ ) # TODO: Force filename expansion on each
item in ${@} or ${arguments[@]}
"${arguments[@]}"
setopt localoptions
}
touch .hidden_file
# Does NOT show .hidden_file
ls -1d *(md-1)
# DOES show .hidden_file
noglob globdots ls -1d *(md-1)
However, I'm not sure what portion of Section 14 (Expansion) to read to
figure out how that works. It initially looked like the (e) flag (per
14.3.2.22) would do this, but it doesn't appear to.
I can just invoke "eval" on something that I KNOW will be a glob expression
to get it to expand, but blindly calling eval will execute things that are
NOT glob expressions, and I only want filename expansion.
What I have found that DOES work (but overkill and a hack) is just to shell
out to zsh again, while passing in all options that are currently set --
plus globdots.
() {
set -x
yo() {
local myopts=( $(setopt) globdots )
local myargs=()
local arg
for arg in "$@"; do
myargs+=( "$(zsh -c "setopt $o &>/dev/null; command echo
$arg" )" )
done
"${myargs[@]}"
}
touch .hidden_file
noglob yo echo *hidden*(mm-1)
}
This of course is wasteful, and I expect there's a better solution.
Then there's also the issue of causing filename expansion to occur when
unintended -- for example, if '*' is indeed just a string argument, and
should not be subject to expansion. I think this corner case I can ignore,
as any user will explicitly be asking for glob expansion.
*Finally, I wanted to say that I genuinely appreciate the help and answers
I've gotten from this community. You're all very welcoming, experienced,
and get down to the point / answers quickly. If there's any way that I can
help support Zsh development or the community around it, please let me
know.*
*Zach Riggle*
[-- Attachment #2: Type: text/html, Size: 4341 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
* FW: Setting GLOB_DOTS for a single command
2021-11-21 4:46 Setting GLOB_DOTS for a single command Zach Riggle
@ 2021-11-21 5:11 ` agkozak
2021-11-21 5:20 ` Bart Schaefer
2021-11-21 5:28 ` Lawrence Velázquez
2021-11-21 5:36 ` Bart Schaefer
2 siblings, 1 reply; 7+ messages in thread
From: agkozak @ 2021-11-21 5:11 UTC (permalink / raw)
To: zsh-users
Zach Riggle wrote:
> I recently learned about GLOB_DOTS, which is pretty useful in some scenarios, e.g.
>
> echo *(md-1)
>
> To show all files modified within the last day. However, in some cases I want to see hidden files (e.g. ./.foo) as well. The globdots option is excellent for > this!
> Is there an easy way to set this for a single expression?
Have you tried
echo *(Dmd-1)
? I think that may be what you're looking for.
Alex
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: FW: Setting GLOB_DOTS for a single command
2021-11-21 5:11 ` FW: " agkozak
@ 2021-11-21 5:20 ` Bart Schaefer
0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2021-11-21 5:20 UTC (permalink / raw)
To: Zsh Users
On Sat, Nov 20, 2021 at 9:11 PM <agkozak@gmail.com> wrote:
>
> echo *(Dmd-1)
Indeed, I'm genuinely curious how Zach managed to learn (md-1) without
stumbling across (D).
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Setting GLOB_DOTS for a single command
2021-11-21 4:46 Setting GLOB_DOTS for a single command Zach Riggle
2021-11-21 5:11 ` FW: " agkozak
@ 2021-11-21 5:28 ` Lawrence Velázquez
2021-11-24 2:20 ` Zach Riggle
2021-11-21 5:36 ` Bart Schaefer
2 siblings, 1 reply; 7+ messages in thread
From: Lawrence Velázquez @ 2021-11-21 5:28 UTC (permalink / raw)
To: Zach Riggle; +Cc: zsh-users
On Sat, Nov 20, 2021, at 11:46 PM, Zach Riggle wrote:
> I recently learned about GLOB_DOTS, which is pretty useful in some
> scenarios, e.g.
>
>> echo *(md-1)
>
> To show all files modified within the last day. However, in some cases
> I want to see hidden files (e.g. ./.foo) as well. The globdots option
> is excellent for this!
>
> Is there an easy way to set this for a single expression?
Yes, the "D" glob qualifier.
% touch {.,}{a..c}
% print -r -- *
a b c
% print -r -- *(D)
.a .b .c a b c
> I know I can use a function / anonymous function combined with
> LOCAL_OPTIONS to get this, but I wondered if there's something more
> clever.
>
> [...]
>
> I expect that there's something I can do for scripts that I fully
> control, to declare some function ('globdots') which is invoked with
> noglob, and can then internally set the flag, and then trigger
> expansion of each argument in "$@".
>
> [...]
>
> I can just invoke "eval" on something that I KNOW will be a glob
> expression to get it to expand, but blindly calling eval will execute
> things that are NOT glob expressions, and I only want filename
> expansion.
>
> What I have found that DOES work (but overkill and a hack) is just to
> shell out to zsh again, while passing in all options that are currently
> set -- plus globdots.
>
> [...]
>
> Then there's also the issue of causing filename expansion to occur when
> unintended -- for example, if '*' is indeed just a string argument, and
> should not be subject to expansion. I think this corner case I can
> ignore, as any user will explicitly be asking for glob expansion.
What verve! Fortunately you don't have to do any of this :)
> Finally, I wanted to say that I genuinely appreciate the help and
> answers I've gotten from this community. You're all very welcoming,
> experienced, and get down to the point / answers quickly.
On behalf of the actual helpful people, you're quite welcome.
> If there's any way that I can help support Zsh development or the
> community around it, please let me know.
I think your using zsh is already quite supportive!
--
vq
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Setting GLOB_DOTS for a single command
2021-11-21 4:46 Setting GLOB_DOTS for a single command Zach Riggle
2021-11-21 5:11 ` FW: " agkozak
2021-11-21 5:28 ` Lawrence Velázquez
@ 2021-11-21 5:36 ` Bart Schaefer
2021-11-21 5:38 ` Bart Schaefer
2 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2021-11-21 5:36 UTC (permalink / raw)
To: Zach Riggle; +Cc: Zsh Users
Answering not the question in the subject, but the question in the
attempted approach:
On Sat, Nov 20, 2021 at 8:47 PM Zach Riggle <zachriggle@gmail.com> wrote:
>
> I expect that there's something I can do for scripts that I fully control, to declare some function ('globdots') which is invoked with noglob, and can then internally set the flag, and then trigger expansion of each argument in "$@".
>
> However, I'm not sure what portion of Section 14 (Expansion) to read to figure out how that works.
Since $@ is a parameter (one form of the array of all positional
parameters) you want to look at 14.3 Parameter Expansion. In
particular
As has been explained already, that's not necessary for globdots.
However, for various reasons, I prefer not to have extendedglob set
all the time, so I have this in my startup:
eglob() {
setopt localoptions extendedglob
local c=$1
shift
$c $~*
}
alias eglob="noglob eglob "
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Setting GLOB_DOTS for a single command
2021-11-21 5:36 ` Bart Schaefer
@ 2021-11-21 5:38 ` Bart Schaefer
0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2021-11-21 5:38 UTC (permalink / raw)
To: Zach Riggle; +Cc: Zsh Users
On Sat, Nov 20, 2021 at 9:36 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> In
> particular
Sorry, an intended copy-paste there didn't get pasted:
${~SPEC}
Turn on the GLOB_SUBST option for the evaluation of SPEC; if the
'~' is doubled, turn it off. When this option is set, the string
resulting from the expansion will be interpreted as a pattern
anywhere that is possible, such as in filename expansion and
filename generation and pattern-matching contexts
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Setting GLOB_DOTS for a single command
2021-11-21 5:28 ` Lawrence Velázquez
@ 2021-11-24 2:20 ` Zach Riggle
0 siblings, 0 replies; 7+ messages in thread
From: Zach Riggle @ 2021-11-24 2:20 UTC (permalink / raw)
To: Lawrence Velázquez; +Cc: Zsh Users
[-- Attachment #1: Type: text/plain, Size: 2502 bytes --]
Indeed, it does appear that the D modifier does what I want!
Thanks Alex, Bart, and Lawrence.
*Zach Riggle*
On Sat, Nov 20, 2021 at 11:28 PM Lawrence Velázquez <larryv@zsh.org> wrote:
> On Sat, Nov 20, 2021, at 11:46 PM, Zach Riggle wrote:
> > I recently learned about GLOB_DOTS, which is pretty useful in some
> > scenarios, e.g.
> >
> >> echo *(md-1)
> >
> > To show all files modified within the last day. However, in some cases
> > I want to see hidden files (e.g. ./.foo) as well. The globdots option
> > is excellent for this!
> >
> > Is there an easy way to set this for a single expression?
>
> Yes, the "D" glob qualifier.
>
> % touch {.,}{a..c}
> % print -r -- *
> a b c
> % print -r -- *(D)
> .a .b .c a b c
>
> > I know I can use a function / anonymous function combined with
> > LOCAL_OPTIONS to get this, but I wondered if there's something more
> > clever.
> >
> > [...]
> >
> > I expect that there's something I can do for scripts that I fully
> > control, to declare some function ('globdots') which is invoked with
> > noglob, and can then internally set the flag, and then trigger
> > expansion of each argument in "$@".
> >
> > [...]
> >
> > I can just invoke "eval" on something that I KNOW will be a glob
> > expression to get it to expand, but blindly calling eval will execute
> > things that are NOT glob expressions, and I only want filename
> > expansion.
> >
> > What I have found that DOES work (but overkill and a hack) is just to
> > shell out to zsh again, while passing in all options that are currently
> > set -- plus globdots.
> >
> > [...]
> >
> > Then there's also the issue of causing filename expansion to occur when
> > unintended -- for example, if '*' is indeed just a string argument, and
> > should not be subject to expansion. I think this corner case I can
> > ignore, as any user will explicitly be asking for glob expansion.
>
> What verve! Fortunately you don't have to do any of this :)
>
> > Finally, I wanted to say that I genuinely appreciate the help and
> > answers I've gotten from this community. You're all very welcoming,
> > experienced, and get down to the point / answers quickly.
>
> On behalf of the actual helpful people, you're quite welcome.
>
> > If there's any way that I can help support Zsh development or the
> > community around it, please let me know.
>
> I think your using zsh is already quite supportive!
>
> --
> vq
>
[-- Attachment #2: Type: text/html, Size: 3361 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2021-11-24 2:21 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-21 4:46 Setting GLOB_DOTS for a single command Zach Riggle
2021-11-21 5:11 ` FW: " agkozak
2021-11-21 5:20 ` Bart Schaefer
2021-11-21 5:28 ` Lawrence Velázquez
2021-11-24 2:20 ` Zach Riggle
2021-11-21 5:36 ` Bart Schaefer
2021-11-21 5:38 ` Bart Schaefer
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).