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

Warn on ambiguous redirect #1136

Open
chutz opened this issue Mar 5, 2018 · 9 comments
Open

Warn on ambiguous redirect #1136

chutz opened this issue Mar 5, 2018 · 9 comments

Comments

@chutz
Copy link

chutz commented Mar 5, 2018

Older versions of bash error out with an ambiguous redirect (ie parameters passed after redirect). Newer versions of bash apparently accept it, and just pass the args to the command.

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

#!/bin/bash
cmd > output.log --long-argument1 --long-argmuent2 param1 param2

This also works in newer bash versions (!?!)

#!/bin/bash
> file.txt echo one two three four

The file will contain "one two three four"

Both of these are just confusing and should be warned about.

Here's what shellcheck currently says:

$ shellcheck myscript
No issues detected!

Here's what I wanted or expected to see:

warn about an ambiguous redirect

@nihilus
Copy link

nihilus commented Mar 5, 2018

Since the parameters are not in order you will get an error ofc.

@chutz
Copy link
Author

chutz commented Mar 5, 2018

Not with bash 4.4, it just works

@nihilus
Copy link

nihilus commented Mar 5, 2018

No it wont work since your example is wrong.

@nihilus
Copy link

nihilus commented Mar 5, 2018

Unless --long-argument2 takes two parameters.

@chutz
Copy link
Author

chutz commented Mar 5, 2018

It does work on bash-4.4, the shell will pass the parameters to the command, as one would not expect. This is why I am suggesting that shellcheck warn on this, it's just confusing.

param1 param2 may just be parameters, like ls -l --human-readable file1 file2

@nihilus
Copy link

nihilus commented Mar 5, 2018

Ah, beats me. Yeah that is indeed ambiguous.

@chutz
Copy link
Author

chutz commented Mar 5, 2018

For added ambiguous-ness

#!/bin/bash
> file.txt echo foo bar baz

Will result in foo bar baz being in file.txt

@koalaman
Copy link
Owner

koalaman commented Mar 6, 2018

This behavior is POSIX and supported by all bash versions. "Ambiguous redirect" is instead the error you get when you redirect to a word that becomes more than one due to globbing or wordsplitting:

true > *
foo="bar baz";  true > $foo

ShellCheck already warns about quoting, but it doesn't warn against redirecting to globs, which it really should.

I'm not so sure about a general warning about not having redirections last though. Things like echo >> file "foo bar" makes lines align nicely and < file grep foo mimics the order of cat file | grep foo

@Dmole
Copy link

Dmole commented May 15, 2018

I'm in favor of "redirections last" a large amount of shellcheck is already about making valid code legible / sane and redirects anywhere but last are bad IMO...I'm also in favor of conditions first (in crazy languages that support "x... if y")

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

4 participants