2

I have a bash script that attempts to synchronize the time on another machine. It is not my source code, but as I am refactoring bash script, I am trying to get it to pass ShellCheck.

The source code for it looks something like this:

d=$(date -u +%m%d%H%M%Y.%S) ssh decs@host-zc1 "sudo date -u ${d}"

The ShellCheck warning is SC2029 and states "Note that, unescaped, this expands on the client side.". In fact I don't want to escape this.

Is there another way to get rid of this warning and yet perform this step?

Kris
  • 363
  • 3
  • 11

1 Answers1

0

When you've determined that a warning does not apply to you, you can ignore it with a # shellcheck disable comment:

d=$(date -u +%m%d%H%M%Y.%S)
# shellcheck disable=SC2029
ssh decs@host-zc1 "sudo date -u ${d@Q}"

Though you should really be using ${d@Q} or printf %q to ensure the name will be properly escaped.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • 1
    Can you give the ```${d@Q}``` and/or ```printf %q``` as the example solution. I'd really like to avoid disabling the ShellCheck warning. There is a reason why it is coming up as a warning. – Kris Dec 07 '18 at 19:59
  • 1
    @Kris You're get an info level message whose [wiki page](https://github.com/koalaman/shellcheck/wiki/SC2029) says "If you do want your string expanded on the client side, you can safely ignore this message". Presumably there are just a lot of cases of people writing `ssh host "echo $HOSTNAME"` and being confused. There does not appear to be a workaround without a comment, which is really too bad. – that other guy Dec 07 '18 at 22:58
  • Yep, I was looking at the ***Exception*** section at the link: https://github.com/koalaman/shellcheck/wiki/SC2029 Anyway, I will look at writing it a different way. I just didn't understand what ```${d@Q}``` mean and what it would do exactly. – Kris Dec 10 '18 at 14:59