5

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?

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
felipe Salomao
  • 313
  • 2
  • 8
  • 21
  • 2
    Sorry - it's not clear to me what you want to achieve. – suspectus Mar 14 '13 at 22:51
  • 1
    why on earth are you putting a concatenated list of filenames into a variable in the first place? – ormaaj Mar 15 '13 at 00:00
  • 1
    Another interesting mess: `a="*"; echo $a; a="'*'"; echo $a` -- no easy way to disable wildcard expansion without introducing other issues, think `if [ -n "$1" ]; then a="-name $1";fi; find /dir $a` (Arrays also works for that case...) – Gert van den Berg Jun 17 '16 at 07:03

3 Answers3

6

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

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
4

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
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
1

Use double quotes:

diretorio=$(echo "$Var2"*)

Single ones prevent variable substitution

jlliagre
  • 29,783
  • 6
  • 61
  • 72