Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In fish shell, how to iterate over files using wildcard and variable?

Tags:

fish

If I run:

for f in *1000.png
   echo $f
end

I get

1000.png
11000.png
21000.png

I would like to do something like:

for i in 1 2 3
    for f in *$i000.png
         echo $f
    end
end

To get

1000.png
11000.png
21000.png
2000.png
12000.png
22000.png
3000.png
13000.png
23000.png

Instead, it outputs nothing.

I also tried:

for i in 1 2
    set name "*"$i"000.png"
    for f in (ls $name)
        echo $f
    end
end

Outputting:

ls: *1000.png: No such file or directory
ls: *2000.png: No such file or directory
like image 275
arsenius Avatar asked Aug 08 '17 02:08

arsenius


1 Answers

To avoid trying to expand the variable $i000, you would do

for i in 1 2 3
    for f in *{$i}000.png
         echo $f
    end
end

Or, avoid the outer loop altogether:

for f in *{1,2,3}000.png
like image 198
glenn jackman Avatar answered Sep 26 '22 03:09

glenn jackman