Shellsheck is a static analysis tool for shell scripts which can be installed local on some Linux systems and can used without installation online for checking bash scripts for some errors.
- https://github.com/koalaman/shellcheck
- https://www.shellcheck.net/
- https://man.archlinux.org/man/shellcheck.1.en
Testing envirement:
- Linux Mint (Ubuntu edition)
- Given are a working bash script which which echo "I a echo from main file" and a source file which echo "I am a echo from source file".
- Booth file are located in same folder.
- tested with shellcheck 0.7.1-1, by local installed version.
main.sh
#!/bin/bash
source ./sourcefile.sh
echo "Output from main.sh"
echo
echo
fkt_output_from_sourcefile_sh
echo
echo
echo "For closing window, press Enter or Ctl + C"; read -r
sourcefile.sh
#!/bin/bash
fkt_output_from_sourcefile_sh() {
echo "Output from sourcefile.sh"
}
How did I run it on terminal:
shellcheck -x main.sh
Output on terminal (looks working fine):
Output from main.sh
Output from sourcefile.sh
For closing window, press Enter or Ctl + C
Error Message by check by shellcheck -x:
In /home/user/desktop/main.sh line 8:
source ./sourcefile.sh
^-- SC1091: Not following: ./sourcefile.sh: openBinaryFile: does not exist (No such file or directory)
Possible solution (which did not work for me, probably depend on my wrong syntax):
- https://github.com/koalaman/shellcheck/wiki/SC1090
- https://github.com/koalaman/shellcheck/wiki/SC1091
- https://github.com/koalaman/shellcheck/issues/769
- https://github.com/koalaman/shellcheck/wiki/Directive
Sample of not working solution:
Based on: "Tell ShellCheck where to find a sourced file (since 0.4.0):"
# shellcheck source=src/examples/config.sh
. "$(locate_config)"
Source:
main.sh
#!/bin/bash
# shellcheck source=./sourcefile.sh
source "$(find_install_dir)/sourcefile.sh"
echo "Output from main.sh"
echo
echo
fkt_output_from_sourcefile_sh
echo
echo
echo "For closing window, press Enter or Ctl + C"; read -r
sourcefile.sh :
#!/bin/bash
fkt_output_from_sourcefile_sh() {
echo "Output from sourcefile.sh"
}
Error message on terminal:
/home/user/desktop/main.sh: Line 4: find_install_dir: Command not found.
/home/user/desktop/main.sh: Line 4: /sourcefile.sh: File or folder not found
Output from main.sh
/home/user/desktop/main.sh: Line 10: fkt_output_from_sourcefile_sh: Command not found.
For closing window, press Enter or Ctl + C