Quantcast
Channel: Active questions tagged fish - Stack Overflow
Viewing all articles
Browse latest Browse all 130

fish shell: Glob function?

$
0
0

When I migrated to fish from bash, I found glob patterns like "tmp[0-9][0-9].pdf" aren't supported. Then, recently fish has stopped to support the ? wildcard:

$ fish --versionfish, version 4.0.0$ ls tmp00.pdftmp00.pdf$ ls tmp??.pdfls: tmp??.pdf: No such file or directory$

https://fishshell.com/docs/4.0/fish_for_bash_users.htmlsays, "Fish only supports the * and ** glob (and the deprecated ? glob) as syntax."

So, has somebody written a fish function that can simulate the good-old glob? which might be used like

for file in (fmatch 'img..\.jpg') # to get img00.jpg, img01.jpg, . . .   # do something on $file

With some help from ChatGPT, I've been able to write something that filters the output from '*' with string match -r -- $regex -- $file, but the handling of paths is incredibly hard to me:

# bashfor file in ../../*/img??.jpg # How to implement this?

and also the constant need of escaping . in regex is tedious, especially when your path includes ../.

I tried fd -g <glob pattern> but this glob doesn't seem to allow for paths.

I need the good old Glob. Perhaps I should admit the defeat :-) and do this

function glob --description 'Good old glob'    if test (count $argv) -ne 1        echo "Usage: glob <glob_pattern>"        return 1    end    bash -c 'printf "%s\n" $*' _ $argv[1] # call bash!end

Edit: Initially, I wrote

bash -c "ls $argv[1]"

But a better version was proposed by @CharlesDuffy, which I've replaced mine with, in the above fish function. [Please refer to @CharlesDuffy's discussion below for why printf and what problems exist in $*. But, given that the above solution still has some problems, it's probably better to install this glob command instead of writing a fish function like the above.]

I'm already using the glob.fish function defined as above. So far so good. glob tmp[0-9][0-9].pdf works!


Viewing all articles
Browse latest Browse all 130

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>