Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt in read -p promt interpreted as variable name #1599

Open
4 tasks done
schaetzc opened this issue Jun 5, 2019 · 2 comments
Open
4 tasks done

Prompt in read -p promt interpreted as variable name #1599

schaetzc opened this issue Jun 5, 2019 · 2 comments

Comments

@schaetzc
Copy link

schaetzc commented Jun 5, 2019

Bug Template

Bug (false negative)

  • Rule Id: SC2154, SC2034
  • My shellcheck version:
    shellcheck --version says 0.6.0, my package manager pacman -Qi says 0.6.0-120
  • The rule's wiki page does not already cover this
  • I tried on shellcheck.net and verified that this is still a problem on the latest commit

For new checks

Here's a snippet or screenshot that shows the problem:

The following script will print the literal string a and read input to the default variable REPLY. Afterwards the unassigned variable $a is accessed.

#! /bin/bash
read -r -p a
echo "$a"

Here's what shellcheck currently says:

There is no output. Shellcheck doesn't find a problem even though there is one (false negative).

Here's what I wanted or expected to see:

echo "$a"
      ^-- SC2154: a is referenced but not assigned.

More Information

Shellcheck seems to misinterpret the prompt argument given to read's -p option as a variable name. I did some further experiments:

For the script

#! /bin/bash
read -r -p a
echo "$REPLY"

shellcheck prints a warning even though there is no problem (false positive).

read -r -p a
           ^-- SC2034: a appears unused. Verify use (or export if used externally).

I expected shellcheck to remain silent.

For the script

#! /bin/bash
read -r -p a b
echo "$a"
echo "$b"

shellcheck remains silent even though there is a problem (false negative). I expected the output

echo "$a"
      ^-- SC2154: a is referenced but not assigned.
@schaetzc
Copy link
Author

schaetzc commented Jun 5, 2019

Some further experiments showed that also other non-variable parameters are interpreted as variable names. This includes (but is not necessarily limited to) read -d a, read -i a and read -u a – in each of these cases shellcheck interprets a as a variable name even though it is a delimiter, initial text, or file descriptor.

Note that a has to be a valid variable identifier for shellcheck to give the false positive SC2034 (appears unused) and the false negative, that is, missing warning SC2154 (referenced but not assigned).

@svanzoest
Copy link

svanzoest commented Aug 28, 2020

I am getting a similar error on the following (with shellcheck 0.4.6, so may already addressed in newer versions, passes fine in v0.7.1+git109.c9be7ab)

  while read -a files -r
  do
    if ! downloadFile "${files[0]}" "${files[1]}"; 
    then
      ((++downloadFailures))
      echo "downloadFailures ${downloadFailures}"
    fi

  done < "./filelist.txt"

^-- SC2154: files is referenced but not assigned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants