[bash] source command not found in sh shell

I have a script that uses sh shell. I get an error in the line that uses the source command. It seems source is not included in my sh shell.

If I explicitly try to run source from shell I get:

sh: 1: source: not found

Should I somehow install "source"? Do I have a wrong version of sh?

This question is related to bash shell sh

The answer is


This may help you, I was getting this error because I was trying to reload my .profile with the command . .profile and it had a syntax error


The source command is built into some shells. If you have a script, it should specify what shell to use on the first line, such as:

#!/bin/bash

In Bourne shell(sh), use the . command to source a file

. filename

Bourne shell (sh) uses PATH to locate in source <file>. If the file you are trying to source is not in your path, you get the error 'file not found'.

Try:

source ./<filename>

I found in a gnu Makefile on Ubuntu, (where /bin/sh -> bash)

I needed to use the . command, as well as specify the target script with a ./ prefix (see example below)

source did not work in this instance, not sure why since it should be calling /bin/bash..

My SHELL environment variable is also set to /bin/bash

test:
    $(shell . ./my_script)

Note this sample does not include the tab character; had to format for stack exchange.


On Ubuntu, instead of using sh scriptname.sh to run the file, I've used . scriptname.sh and it worked! The first line of my file contains: #!/bin/bash

use this command to run the script

.name_of_script.sh

source is a bash built-in command so to execute source command, you can log in as Root.

sudo -s source ./filename.sh


This problem happens because jenkins Execute Shell runs the script via its /bin/sh

Consequently, /bin/sh does not know "source"

You just need to add the below line at the top of your Execute Shell in jenkins

#!/bin/bash

$ls -l `which sh`
/bin/sh -> dash

$sudo dpkg-reconfigure dash #Select "no" when you're asked
[...]

$ls -l `which sh`
/bin/sh -> bash

Then it will be OK


The source builtin is a bashism. Write this simply as . instead.

e.g.

. $FILE

# OR you may need to use a relative path (such as in an `npm` script):

. ./$FILE

https://wiki.ubuntu.com/DashAsBinSh#source


I faced this error while i was trying to call source command from #Jenkins execute shell.

source profile.txt or source profile.properties

Replacement for source command is to use,

. ./profile.txt or . ./profile.properties

Note: There is a space between the two dots(.)


Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to shell

Comparing a variable with a string python not working when redirecting from bash script Get first line of a shell command's output How to run shell script file using nodejs? Run bash command on jenkins pipeline Way to create multiline comments in Bash? How to do multiline shell script in Ansible How to check if a file exists in a shell script How to check if an environment variable exists and get its value? Curl to return http status code along with the response docker entrypoint running bash script gets "permission denied"

Examples related to sh

How to run a cron job inside a docker container? I just assigned a variable, but echo $variable shows something else How to run .sh on Windows Command Prompt? Shell Script: How to write a string to file and to stdout on console? How to cat <<EOF >> a file containing code? Assigning the output of a command to a variable What does set -e mean in a bash script? Get specific line from text file using just shell script Printing PDFs from Windows Command Line Ubuntu says "bash: ./program Permission denied"