How do you fix or mask a 1091 error found with shellcheck when including a source in a bash script?

5.9k Views Asked by At

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.

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):

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
2

There are 2 best solutions below

5
Mahdy Mirzade On BEST ANSWER

I reviewed your situation better, and found this issue on shellcheck's github: https://github.com/koalaman/shellcheck/issues/1356 (This specific comment)

From what I understood, there are 2 solutions mentioned from developers:

1. Solving SC1091:
Because this error wasn't shown to me again with your edited code as well, and I don't see your shellcheck comment about SC1091 above source ./sourcefile.sh, I only can suggest you to add this comment in main.sh, and check again:

...
# shellcheck disable=SC1091
source ./sourcefile.sh
...

Then run:

$ shellcheck main.sh

2. Read this if shellcheck was installed using snap:
snap will block access to any other directories that are not in /home//media but looking into your logs it seems like your scripts are in /home/user so that's not a problem, it may be #1.

0
Filip Seman On

Use source-path with SCRIPTDIR, see docs

The special value source-path=SCRIPTDIR will search in the current script's directory, and it can be used as a relative path like source-path=SCRIPTDIR/../lib.

# shellcheck source-path=SCRIPTDIR
source ./sourcefile.sh