27

I have a file to be sourced in Centos 7. It just works fine if I do :

$ source set_puregev_env

however, if I put this in a shell script, it doesn't work..

$ sh xRUN 
xRUN: line 3: source: set_puregev_env: file not found

this is my shell script : xRUN

#!/bin/bash

source set_puregev_env

can anyone tell me what I might be doing wrong, or missing?

3
  • btw, this file is -rw-r--r--. 1 Commented Feb 14, 2018 at 10:51
  • What’s the path to set_puregev_env and what is the working directory when you run the script? Commented Feb 14, 2018 at 10:52
  • 3
    I’m not so sure about the duplicate: It looks to me like source runs but can’t find the file. My proverbial money is on the file not being in the current directory. Commented Feb 14, 2018 at 11:46

1 Answer 1

22

source is a command implemented in bash, but not in sh. There are multiple ways to fix your script. Choose either one.

Run the script using bash interpreter

When you are invoking the xRUN script - you are explicitly telling it to be interpreted by sh

$ sh xRUN 

To change and interpret the script with bash instead do

$ bash xRUN 

This will make bash interpret the source command, and your script will work.

Use dot command to make script bourne compatible

You can also change the source with a dot command which does the same thing but is supported in both bourne and bash.

Change the line:

source set_puregev_env

With:

. set_puregev_env 

Now the script will work with either sh or bash.

Make script executable

You should also run the script directly to avoid confusions like these by making it executable chmod +x xRUN, and invoking it like this:

$ ./xRUN

It will then use the command specified in the shebang and use the rest of the script as input. In your case it will use bash - since that is specified in the shebang.

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

2 Comments

In ubuntu sh is a symbolic link to a command interpreter, bash, dash or ash usually.
on mac os x bash: GNU bash, version 3.2.57(1)-release (arm64-apple-darwin22) source file throws an error: -bash: ????: command not found and source file.tmp or source file.sh works fine. (without x bit and she-bang) source ./file - works as well. Looks like it checks extention too. Weird.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.