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

encourage using 'basename' always with -- #1523

Open
bittorf opened this issue Mar 17, 2019 · 1 comment
Open

encourage using 'basename' always with -- #1523

bittorf opened this issue Mar 17, 2019 · 1 comment

Comments

@bittorf
Copy link

bittorf commented Mar 17, 2019

proposal:

#!/bin/sh
file='-/a/b/c.de'
basename "$file"

this throws an error:

# basename "$file"
basename: invalid option -- '/'
Try 'basename --help' for more information.

so better remind the use to always call with

basename -- "$file"

maybe related: #9 and
https://unix.stackexchange.com/questions/94295/shellcheck-is-advising-not-to-use-basename-why/381440

Gandalf- added a commit to Gandalf-/shellcheck that referenced this issue Mar 24, 2019
Addresses issue koalaman#1523

Checks if '--' should be the first argument to basename, to protect
against filenames being interpreted as options.
@tripleee
Copy link

As already commented on the PR, this is by no means specific to basename, and so the check should probably be extended to any command which accepts a file name parameter. basename is a common victim to this because people do

for filename in *; do
    basename "$filename"
done

but the proper fix here is to say

for filename in ./*; do

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