Shellcheck: "appears unused" false positive with aliases (!)

Created on 11 Feb 2020  Â·  2Comments  Â·  Source: koalaman/shellcheck

Hey @koalaman & team!

Love the tool — huge thanks for making it.

Reporting a peculiar (but real!) edge-case for your pleasure. When checking a script which uses aliases, you either get the awesome SC2139 with alias body in double quotes — or enclose the body properly in single quotes — and then, shellcheck misses variable use in the alias.

TL;DR: just see the reproducer, carefully extracted from a big fat "in-the-wild" script.

For bugs

  • Rule Id (if any, e.g. SC1000): SC2034
  • My shellcheck version (shellcheck --version or "online"): 0.7.0
  • [x] The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
  • [x] I tried on shellcheck.net and verified that this is still a problem on the latest commit

Reproducer

#!/bin/bash

SSH_OPTS="
    -o PasswordAuthentication=no
    -o UserKnownHostsFile=/dev/null
    -o StrictHostKeyChecking=no
    -o LogLevel=quiet
    "

shopt -s expand_aliases

alias ohsodirtyssh='ssh -t $SSH_OPTS'

# ...
ohsodirtyssh [email protected] echo "well i'm an example..."

Here's what shellcheck currently says:

In test.sh line 3:
SSH_OPTS="
^------^ SC2034: SSH_OPTS appears unused. Verify use (or export if used externally).

For more information:
  https://www.shellcheck.net/wiki/SC2034 -- SSH_OPTS appears unused. Verify u...

Here's what I wanted or expected to see:

No warning, the variable is in use.

Most helpful comment

There was a hack in place to look up variables from string literals in trap commands, and now the same thing is done for aliases. It's not foolproof, but it covers the most trivial cases like this. Thanks!

All 2 comments

I hit this one too, ironically after following https://github.com/koalaman/shellcheck/wiki/SC2139 to change double to single quotes

There was a hack in place to look up variables from string literals in trap commands, and now the same thing is done for aliases. It's not foolproof, but it covers the most trivial cases like this. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maxisme picture maxisme  Â·  3Comments

bbarker picture bbarker  Â·  3Comments

ghost picture ghost  Â·  4Comments

koalaman picture koalaman  Â·  4Comments

tdmalone picture tdmalone  Â·  3Comments