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!

2
  • 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). Commented Jun 26, 2020 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
    Commented Jun 29, 2020 at 1:35

1 Answer 1

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?

2
  • Thank you for your input!
    – ganq
    Commented Jun 29, 2020 at 1:15
  • 1
    Note that globbing is not performed in array=([0]=* [1]=???) (ksh93 syntax) either. Quoting would be needed in array=('*' '???') simple zsh-like syntax. Commented Jun 29, 2020 at 9:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .