Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Running ls with wildcard string

I was originally using this command, and it works fine (counting number of files with extension .sb):

ls -dq *.sb | wc -l

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

903

Now, I want to use a variable to store the string, like this:

search="*.sb"

Then, putting it all together:

# count files in directory with substring
search="*.sb"
ls -dq "$search" | wc -l

Output:

ls: cannot access *.sb: No such file or directory
0

This implies the string is being stored and retrieved correctly, but the command is not acting as expected. Could anyone explain this phenomenon to me?

>Solution :

Variable expansion is done before pathname expansion. See here for a similar situation.

Solution in this case: remove the quotation marks

search="*.sb"
ls -dq $search | wc -l
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading