5

I am working with a bash script that someone else wrote and I see the following line:

cp -v  ${LOG_DIR}/${APPLICATION}\.*.log ${ARCHIVED_LOG_DIR}

The files with which it's working are all named like:

EXAMPLE.command1.log EXAMPLE.command2.log

Is there any reason for the backslash escaping the dot since a dot isn't treated specially in filename expansions? What are the implications of doing this vs without the backslash as such?:

cp -v  ${LOG_DIR}/${APPLICATION}.*.log ${ARCHIVED_LOG_DIR}
  • 1
    Since . is not a character that needs quoting, it looks like the backslash is entirely extraneous. It may be a typo, or the person who wrote that line just felt like adding a backslash? – jw013 Nov 5 '12 at 18:35
  • I agree that it was likely a typo but it made me curious whether there were any effective differences in the two commands or if the backslash is simply ignored. – Dave Forgac Nov 5 '12 at 18:40
3

There is no difference. The dot . is not a special shell character that needs quoting, and the backslash will simply be removed during quote removal. This is true in most shells, not just bash.

  • Thanks, this is the supporting documentation for which I was looking. – Dave Forgac Nov 5 '12 at 18:46
3

Not an answer to your question that has already been addressed by @jw013, but please note that while the \ and {} are superfluous here, there are a few things missing. Like the variables should be quoted, and -- to mark the end of options is missing.

cp -v -- "$LOG_DIR/$APPLICATION".*.log "$ARCHIVED_LOG_DIR"
  • 2
    I was going to mention this in a comment but didn't as it was not relevant to the question. However all these adjustments are indeed necessary for maximum robustness. – jw013 Nov 5 '12 at 19:14
  • 2
    @jw013, agreed, but I think it's worth mentioning. The \ is misleading but harmless, the {} is cosmetic and a matter of taste, but the missing quotes or -- are wrong. – Stéphane Chazelas Nov 5 '12 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.