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

False positives for SC2086 and SC2154 with named coprocess in bash #1066

Open
1 task done
automorphism88 opened this issue Nov 30, 2017 · 2 comments
Open
1 task done

Comments

@automorphism88
Copy link

automorphism88 commented Nov 30, 2017

For bugs

  • Rule Id (if any, e.g. SC1000): SC2086,2154
  • My shellcheck version (shellcheck --version or "online"): 0.4.6 and online
  • I tried on shellcheck.net and verified that this is still a problem on the latest commit

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

#!/bin/bash
set -u
exec 3>&1
coproc my_coproc { sed 's/foo/bar/g' ; } >&3
echo foo >&${my_coproc[1]}
eval "exec ${my_coproc[1]}>&-"
wait $my_coproc_PID

Here's what shellcheck currently says:

Line 5:
echo foo >&${my_coproc[1]}
           ^-- SC2086: Double quote to prevent globbing and word splitting.
 
Line 7:
wait $my_coproc_PID
     ^-- SC2154: my_coproc_PID is referenced but not assigned.
     ^-- SC2086: Double quote to prevent globbing and word splitting.

Here's what I wanted or expected to see:

Neither warning should be displayed. The script is valid and executes successfully with output bar as expected, despite the set -u on line 2.

The SC2154 warning is spurious because creating the named coprocess my_coproc on line 4 assigns the variable my_coproc_PID. And both my_coproc_PID and my_coproc[1] are known to be integers, so there's no need for double quotes or SC2086.

@automorphism88 automorphism88 changed the title False positive for SC2154 with named coprocess PID in bash False positives for SC2086 and 2154 with named coprocess in bash Nov 30, 2017
@automorphism88 automorphism88 changed the title False positives for SC2086 and 2154 with named coprocess in bash False positives for SC2086 and SC2154 with named coprocess in bash Nov 30, 2017
@Nightfirecat
Copy link
Contributor

From http://wiki.bash-hackers.org/syntax/keywords/coproc,

The process ID of the shell spawned to execute the coprocess is available through the value of the variable named by NAME followed by a _PID suffix. For example, the variable name used to store the PID of a coproc started with no NAME given would be COPROC_PID (because COPROC is the default NAME).

@automorphism88
Copy link
Author

automorphism88 commented Nov 30, 2017

The bash manual says:

The format for a coprocess is:

coproc [NAME] command [redirections]
This creates a coprocess named NAME. If NAME is not supplied, the default name is COPROC. NAME must not be supplied if command is a simple command (see Simple Commands); otherwise, it is interpreted as the first word of the simple command.

When the coprocess is executed, the shell creates an array variable (see Arrays) named NAME in the context of the executing shell. The standard output of command is connected via a pipe to a file descriptor in the executing shell, and that file descriptor is assigned to NAME[0]. The standard input of command is connected via a pipe to a file descriptor in the executing shell, and that file descriptor is assigned to NAME[1]. This pipe is established before any redirections specified by the command (see Redirections). The file descriptors can be utilized as arguments to shell commands and redirections using standard word expansions. The file descriptors are not available in subshells.

The process ID of the shell spawned to execute the coprocess is available as the value of the variable NAME_PID. The wait builtin command may be used to wait for the coprocess to terminate.

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