0

Here is an excerpt from my Bash script

#!/bin/sh
# Check SPEED parameter validity
if [ "$4" = "SPEED" ] 
then
    source ${SIM_DIR}/setEnv.sh speed
elif [ "$4" = "NORMAL" ]
then
    pushd ${SIM_DIR}/scripts
    source setEnv
else
    ERROR "Invalid SPEED|NORMAL parameter ($4)."
    exit 1
fi

In command line, I am giving the option as NORMAL when I run the script. There is no file called setEnv.sh in the ${SIM_DIR}/scripts location. There is however a file called setEnv and its first line is #!/bin/bash -x. I get the following error:

line 176: source: setEnv: file not found

Could anybody kindly point out what is wrong with my script?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Müller
  • 973
  • 6
  • 19
  • 38

1 Answers1

2

source uses PATH lookups to find names that do not contain slashes, and your PATH (correctly) does not contain ., so the current directory is not searched for setEnv. Use source ./setEnv.

The shebang line is ignored by source.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • It's worth adding that this behaviour is specific to POSIX mode, which OP might have done accidentally with the `/bin/sh` shebang. Otherwise Bash would check the current dir after the `PATH`. [Docs](https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html) – wjandrea Aug 30 '23 at 18:53