s.a
s.a

Reputation: 9

using asterisk in a variable as extension in bash

I want to define a variable that contain asterisk(*), like this

var=*.fits

The asterisk.fits is not the name of my file, but is name of some files with fits extension.
actually I want to define this variable to use it in the next stage:

ls "$var"

so that I see all files with .fits extension.

How can I do that? It seems that just writing just var=*.fits does not work.

Thanks in advance

Upvotes: 1

Views: 1186

Answers (1)

alecxs
alecxs

Reputation: 731

if you want the wildcard assignment to be stored in variable without globbing word-splitting you must quote it

var='*.fits *.txt'
var=*.fits
ls $var

globbing is not performed during an assignment var= (but word-splitting is)
as you can see you must not quote $var output in order to get it evaluated

the shell expand to whatever it finds at the moment of globbing

ls "$var"
ls: cannot access '*.fits': No such file or directory

ls $var
file1.fits file2.fits file3.fits

Upvotes: 1

Related Questions