Warn about "return" can only in a function or sourced script
"return" can only be used in a function or sourced script in Bash. It will causes an execution error. Here is a simple example.
#!/bin/bash
echo "Hello!"
return 18
echo "Hello you!"
In bash, it will show "Hello!" and "Hello you!" and return error code 0 with message line 3: local: can only be used in a function.
In zsh, it will show only "Hello!" and return error code 18.
May shellcheck check for this?
How would shellcheck know whether the script is being sourced or not?
Shellcheck is never know ... Would this be a default off option?
I think it's safe to say this issue can be closed, due to what @aw-was-here pointed out. (CodeTriage brought me here... I'd like to help cut down on the number of open issues in this project.)
Just leave it. There may be some option in the future.
How would shellcheck know whether the script is being sourced or not?
Yes, make it optional if necessary.
Proposed heuristic :
- the file has a shebang
- the file defines one or more functions
- the return statement is not inside any function context
Duplicate of #281