4

I am writing simple shell script and when I check my script at https://www.shellcheck.net it give me error at line 14

Line 14:
  sysrc ifconfig_"${Bridge}"="addm ${NIC}"
                            ^-- SC2140: Word is of the form "A"B"C" (B indicated). Did you mean "ABC" or "A\"B\"C"?

https://github.com/koalaman/shellcheck/wiki/SC2140

In fact I didn't understand how to correct it

#!/bin/sh

Setup() {
  # Determine interface automatically
  NIC="$(ifconfig -l | awk '{print $1}')"
  # Enabling the Bridge
  Bridge="$(ifconfig bridge create)"
  # Next, add the local interface as member of the bridge.
  # for the bridge to forward packets,
  # all member interfaces and the bridge need to be up:
  ifconfig "${Bridge}" addm "${NIC}" up
  # /etc/rc.conf
  sysrc cloned_interfaces="${Bridge}"
  sysrc ifconfig_"${Bridge}"="addm ${NIC}"

  # Create bhyve startup script
  touch /usr/local/etc/rc.d/bhyve
  chmod +x /usr/local/etc/rc.d/bhyve
  cat << 'EOF' >> /usr/local/etc/rc.d/bhyve
#!/bin/sh
# PROVIDE: bhyve
# REQUIRE: DAEMON
# KEYWORD: shutdown
. /etc/rc.subr
name=bhyve
start_cmd="${name}"_start
bhyve_start() {
}
load_rc_config "${name}"
run_rc_command "$1"
EOF
  sysrc bhyve_enable="YES"
}
1
  • 6
    Kudos for using shellcheck: it's a great tool for learning. – glenn jackman Dec 27 '20 at 3:48
10

The single string

ifconfig_"${Bridge}"="addm ${NIC}"

is the same as

"ifconfig_$Bridge=addm $NIC"

(the curly braces aren't needed and the whole string can be quoted by a single set of double quotes)

Since you used double quotes to quote two separate parts of the same string, ShellCheck wondered whether you possibly meant for the "inner pair" of quotes to be literal and actually part of the string, i.e. whether you meant to write fconfig_"${Bridge}\"=\"addm ${NIC}".

Since you didn't, it would be better to rewrite the string as I showed before, just to make it clear that it's one single string with no embedded quotes.

Note that you have made no error in your code with regard to the quoting here, and that ShellCheck is simply inquiring about your intention because this is (arguably) a common error when you do want literal double quotes inside a string.

If you feel strongly about your way of quoting the string, then you may disable the ShellCheck warning with a directive in a comment before the affected line:

# shellcheck disable=SC2140
sysrc ifconfig_"${Bridge}"="addm ${NIC}"

This basically means "I know what I'm doing and rule SC2140 does not apply here, thank you very much."

4
  • 6
    Although the braces aren't strictly needed, they do no harm and can aid readability: "ifconfig_${Bridge}=addm ${NIC}" – glenn jackman Dec 27 '20 at 3:46
  • 2
    @glennjackman I personally only use the curly braces when actually needed. Some people additionally mistake the use of curly braces for quoting (i.e. they think using the curly braces quotes the variable expansion). – Kusalananda Dec 27 '20 at 7:19
  • Thanks for the answer. I am using curly braces, because I am following google style guide google.github.io/styleguide/shellguide.html – Amr Dec 27 '20 at 12:09
  • @Amr Fair enough, as long as you are consistent. – Kusalananda Dec 27 '20 at 12:23
3

If you follow the suggested explanation link for SC2140, you get this text,

Problematic code:

echo "<img src="foo.png" />" > file.html

or

export "var"="42"

Correct code:

echo "<img src=\"foo.png\" />" > file.html

or

export "var=42"

Look at the second example - it's almost identical structure to your code

sysrc ifconfig_"${Bridge}"="addm ${NIC}"

What you're being told is to check whether the double quotes are supposed to be part of the string, or if you've simply got extra ones you don't need. In your case it's the second option, and this line should probably be better written as

sysrc "ifconfig_${Bridge}=addm ${NIC}"
1
  • Thanks it works – Amr Dec 27 '20 at 12:28

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.