1

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?

6
  • 3
    quote the variables: "${SIM_DIR}" Commented Dec 16, 2016 at 7:30
  • You could try to add a pwd command and an ls command to your script, to see clearly in which directory it is running and which files really are there. Commented Dec 16, 2016 at 7:31
  • 2
    shebang #!/bin/bash -x has no effect if you source a file. Commented Dec 16, 2016 at 7:33
  • @ThomasPadron-McCarthy - already added pwd and ls and it is exactly in the directory where I want to source setEnv from. Commented Dec 16, 2016 at 7:33
  • 1
    I copied your script, and (with #!/bin/bash instead of #!/bin/sh, since the sh on my system doesn't have pushd), it works as expected on my system. Your problem seems to be elsewhere. Commented Dec 16, 2016 at 8:30

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.