Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcard in variable, Shell bash, how to do ? Variable=$variable2* [duplicate]

For me it worked:

diretorio=$(echo 'test 123'*)

but not worked when i used variable in quotes

Var2="test 123" 
diretorio=$(echo '$Var2'*)

How to solve it?

like image 918
felipe Salomao Avatar asked Dec 21 '22 08:12

felipe Salomao


2 Answers

The mistake in your glob is that

diretorio=$(echo '$Var2'*)

is a shot in /dev/null, because the shell don't expand variables in single quotes.

So :

diretorio=$(echo "$Var2"*)

Learn the difference between ' and " and `. See http://mywiki.wooledge.org/Quotes and http://wiki.bash-hackers.org/syntax/words

like image 178
Gilles Quenot Avatar answered Jan 11 '23 22:01

Gilles Quenot


May I suggest an alternate approach? Instead of making a space-separated list of filenames (which will cause horrible confusion if any of the filenames contain spaces, e.g. "test 123"), use an array:

diretorio=("${Var2}"*)
doSomethingWithAllFiles "${diretorio[@]}"
for umDiretorio in "${diretorio[@]}"; do
    doSomethingWithASingleFile "$umDiretorio"
done
like image 41
Gordon Davisson Avatar answered Jan 11 '23 22:01

Gordon Davisson