0

Why this command works:

sudo rm -rf ${server_tomcatHomeDir}temp/*

While the same, but with quoted arg, doesn't work:

sudo rm -rf "${server_tomcatHomeDir}temp/*"

It might be related only to quotes, but why this command works even with quoted args:

sudo cp "$HOME/${artifact}" "${server_tomcatHomeDir}/webapps/${webapp}"

I can't understand. Can anyone clarify?

I just want to use quotes in the first command to prevent globbing and word splitting.

Cyrus
  • 77,979
  • 13
  • 71
  • 125
Victor Dombrovsky
  • 2,525
  • 2
  • 17
  • 31
  • "Doesn't work" -- not so; it works perfectly well at deleting a file named `*` inside the given directory. :) – Charles Duffy Oct 06 '16 at 20:10
  • 1
    Put a `/` before `temp`. It won't hurt to have two `/` if `$server_tomcatHomeDir` ends with a `/`, but your code won't work properly if `$server_tomcatHomeDir` does *not* end with `/`. – chepner Oct 06 '16 at 20:16

2 Answers2

4

The * here is a pathname expansion (glob) token, that will match any number of characters (including zero) in filename, and it is not expanded when put inside quotes, it is treated literally then.

Do:

sudo rm -rf "${server_tomcatHomeDir}"temp/*

Note that, it is almost always a good idea to quote your variables, especially when dealing with file names to avoid word splitting, and pathname expansion.

heemayl
  • 35,775
  • 6
  • 62
  • 69
2

You have to put the * outside of the quotes, so

sudo rm -rf "${server_tomcatHomeDir}temp/"*
Gilles Quenot
  • 154,891
  • 35
  • 213
  • 206