Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"source" command in shell script not working [duplicate]

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?

like image 781
RETELLIGENCE Avatar asked Feb 14 '18 10:02

RETELLIGENCE


People also ask

What is the use of source command in shell script?

In Linux systems, source is a built-in shell command that reads and executes the file content in the current shell. These files usually contain a list of commands delivered to the TCL interpreter to be read and run.

How do I run a source in bash?

The built-in bash source command reads and executes the content of a file. If the sourced file is a bash script, the overall effect comes down to running it. We may use this command either in a terminal or inside a bash script. To obtain documentation concerning this command, we should type help source in the terminal.

What is $# in shell script?

$# : This variable contains the number of arguments supplied to the script. $? : The exit status of the last command executed. Most commands return 0 if they were successful and 1 if they were unsuccessful. Comments in shell scripting start with # symbol.

What is the use of source command in Unix?

source is a shell built-in command which is used to read and execute the content of a file(generally set of commands), passed as an argument in the current shell script. The command after taking the content of the specified files passes it to the TCL interpreter as a text script which then gets executed.


1 Answers

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.

like image 165
stalet Avatar answered Sep 28 '22 20:09

stalet