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

Bad Advice on Replacing "which" with "command -v" - Not Equivalent #1707

Closed
AdamDanischewski opened this issue Sep 28, 2019 · 5 comments
Closed

Comments

@AdamDanischewski
Copy link

For bugs

  • Rule Id: SC2230

  • My shellcheck version (shellcheck --version or "online"): version: 0.5.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

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

Screenshot from 2019-09-28 12-28-30

Here's what shellcheck currently says:

SEQ_CMD="$(which seq)"||MISSING_CMDS+=("seq");declare -r SEQ_CMD
^-- SC2230: which is non-standard. Use builtin 'command -v' instead.

Here's what I wanted or expected to see:

Using command -v vs which is probably bad advice. For instance if an alias exists for a command and you issue a "which command" you will get the full path to the first version in the PATH variable found. If you issue a command -v you will get the alias if one exists. These two commands are not interchangeable.

Try:
$> alias cp='_(){ ((${#}==1)) && cp -nv "${1}" "$(pwd)" || cp -nv "${1}" "${2}";};_'
now, you can see the difference:
$> which cp
/bin/cp
$> command -v cp
alias cp='(){ ((${#}==1)) && cp -nv "${1}" "$(pwd)" || cp -nv "${1}" "${2}";};'

When you are using which to set help set command path variables you don't want it loaded with alias declarations.

@AdamDanischewski
Copy link
Author

AdamDanischewski commented Oct 2, 2019

I guess that was a bad example for files. Instead, try sourcing an alias functions file from another script:

s.bsh:
function cp() { echo hi; }

r.bsh:

#!/bin/bash 
. s.bsh 
which cp
command -v cp

and run:

$> ./r.bsh 
/bin/cp
cp

Here you can see that command -v prints simply the name of the function and which produces the fully qualified path.

@awgeorge
Copy link

awgeorge commented Oct 4, 2019

It also incorrectly suggests to use command -v when using the -a parameter of which

$(which -a open)
  ^---^ SC2230: which is non-standard. Use builtin 'command -v' instead.

@awgeorge
Copy link

awgeorge commented Oct 4, 2019

See also: #1162

@AdamDanischewski
Copy link
Author

@awgeorge Thanks for the update. I'll close this duplicate - and yes, command -v is not a subsitute for which -a.

@AdamDanischewski
Copy link
Author

Duplicate of #1162.

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