mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
7.1k stars 336 forks source link

syntax: give a helpful error message when <<< is used in POSIX mode #881

Closed hbarcelos closed 2 years ago

hbarcelos commented 2 years ago

shfmt is having issues with the herestring operator (<<<).

The minimum reproducible example:

#!/bin/bash
# script.sh
grep 'foo' <<<'foo'

If I run:

shfmt script.sh

I get:

> script.sh:3:12: << must be followed by a word
hbarcelos commented 2 years ago

Looks like <<< is not POSIX, so I needed to change shell_variant to bash, then the issue went away.

mvdan commented 2 years ago

Hrm, we should probably give a better error in that case though, because a POSIX << cannot possibly be followed by a <. We already give a helpful error in other scenarios, like when one tries to use Bash arrays in POSIX mode.

mvdan commented 2 years ago
$ cat array.sh 
foo=(bar)
$ shfmt -ln=bash array.sh 
foo=(bar)
$ shfmt -ln=posix array.sh 
array.sh: array.sh:1:5: arrays are a bash/mksh feature
$ cat herestring.sh 
foo <<< bar
$ shfmt -ln=bash herestring.sh 
foo <<<bar
$ shfmt -ln=posix herestring.sh 
herestring.sh: herestring.sh:1:5: << must be followed by a word

The last one could say something like herestrings are a bash/mksh feature.