sh icon indicating copy to clipboard operation
sh copied to clipboard

syntax: confusing error when "function" is used in posix mode

Open wangyushui opened this issue 3 years ago • 3 comments

  • error: Euler:~ #shfmt_v3.5.1_linux_arm64 -i 4 -ci -d -bn -sr 1.sh 1.sh:2:15: a command can only contain words and redirects; encountered ( Euler:~ #cat 1.sh #!/bin/sh function watch() { true }

  • no problem: Euler:~ #shfmt_v3.5.1_linux_arm64 -i 4 -ci -d -bn -sr 1.sh Euler:~ #cat 1.sh #!/bin/sh watch() { true }

wangyushui avatar Sep 01 '22 02:09 wangyushui

I think you want to be using #!/bin/bash as your shebang, as function is a Bash feature. Or otherwise, explicitly pass in the -ln=bash flag. Although our error message here could probably be more helpful, so I think it's worth keeping this issue open for that improvement.

mvdan avatar Sep 01 '22 21:09 mvdan

Thank you for your reply. I agree with you

I think you want to be using #!/bin/bash as your shebang, as function is a Bash feature. Or otherwise, explicitly pass in the -ln=bash flag. Although our error message here could probably be more helpful, so I think it's worth keeping this issue open for that improvement.

wangyushui avatar Sep 02 '22 01:09 wangyushui

I'll keep this open for the sake of better errors :)

mvdan avatar Sep 02 '22 07:09 mvdan