1

I am new to UNIX and currently working on a shell script where I will be receiving files with names such as abc_123_date.zip so the file names will be abc_123_12312005. Instead of hardcoding abc_123_*.zip in commands such as find and if [ -f ... ] all over, I plan to store that string in a variable and then use it. For example:

file_name=abc_123_*.zip

And then in rest of the sections of the code I can use $file_name instead of the actual string for any and all operations which requires that string.

What is the best way to store that string:

Option 1: file_name=abc_123_*.zip

Option 2: file_name="abc_123_*.zip"

Which is the better option? and why?

Thanks!

AdminBee
  • 22,803
ganq
  • 11
  • 3
  • 2
    The space before the = does not look right. Is it OK on ksh? It is not in bash. (I can't see how it could reliably parse it). – ctrl-alt-delor Jun 26 '20 at 22:23
  • Yes, it's not okay in ksh. I was just trying provide an example on that line. The actual options do not have any. – ganq Jun 29 '20 at 01:35

1 Answers1

3

It's up to preference in that it doesn't matter to the shell as far as the filename wildcards are concerned. Filename expansion and word splitting don't happen in a regular (non-array) assignment, regardless of if the quotes are there or not.

Though in many other contexts you'd need the quotes, and you'd also need them in an assignment if the value contains whitespace or shell operators (e.g. ;, (, > etc.). So some might want to use them here, too, just to use the same custom everywhere.

See also: When is double-quoting necessary?

ilkkachu
  • 138,973