Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell wildcard with parameter expansion

Tags:

bash

shell

I'm a green hand in Shell. Please see my following code. How to make it working?

[root@localhost ~]# ls {*.txt,*.exe}
a.txt  b.txt  a.exe b.exe
[root@localhost ~]# filter={*.txt,*.exe}
[root@localhost ~]# echo $filter
{*.txt,*.exe}
[root@localhost ~]# ls $filter
ls: {*.txt,*.exe}: No such file or directory
[root@localhost ~]# 
like image 202
Just a learner Avatar asked Mar 07 '26 01:03

Just a learner


2 Answers

This should do the trick:

eval ls $filter
like image 55
Jonathan Leffler Avatar answered Mar 09 '26 15:03

Jonathan Leffler


NOTE: Assuming Bash and Linux here in absence of any concrete information.

First of all you can get this via find, e.g.

find -type f -name '*.sql'

... or echo:

echo *.{sql,log}

as well. Many ways to do one thing in *nix shells :)

Secondly you'd have to eval the expression to make use of the globbing feature (globbing is the technical term for wildcard expansion).

You'll also want to enclose the assignment in single quotes to avoid expansion to happen too early:

filter='*.{sql,log}'

then observe:

$ echo $filter
*.{sql,log}

but:

$ eval "echo $filter"
test.sql test.log
like image 32
0xC0000022L Avatar answered Mar 09 '26 15:03

0xC0000022L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!