333

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?

3
  • 2
    One could reasonably argue that a shell which supports source is a "wrong version". Commented Dec 4, 2012 at 12:08
  • 3
    Also, the error message source: not found means that the source command was evaluated properly, but the file it should have read does not exist. Commented Jan 7, 2014 at 6:43
  • 7
    It's not possible to "install" source because it is a feature of the shell. It cannot be implemented as an external command. Commented Mar 30, 2018 at 17:00

15 Answers 15

355

/bin/sh is usually some other shell trying to mimic The Shell. Many distributions use /bin/bash for sh, it supports source. On Ubuntu, though, /bin/dash is used which does not support source. Most shells use . instead of source. If you cannot edit the script, try to change the shell which runs it.

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

7 Comments

Thanks! Replacing /bin/sh with /bin/bash did work on Ubuntu! I'm curious why though, does it mean on Ubuntu bash is sh?
@Milad: On recent Ubuntus, /bin/sh calls /bin/dash. Traditionally, /bin/sh called /bin/bash is sh-compatibility mode.
@Milad I think it does not work on ubuntu 14.04. What is your OS version?
Maybe help known - if you have cascade scripts then rename all "sh -> bash". Thank you. :-)
It wasn't immediately intuitive to me how to fix this issue unfortunately but it set me on the right path. I had to add SHELL := /bin/bash to the top of my Makefile.
|
262

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

. filename

In certain OS's/environments (Mac OS, Travis-CI, Ubuntu, at least) this must be:

. ./filename

(Credit to Adrien Joly's comment below)

10 Comments

I am not really allowed to change the script, it runs fine on a redhat server. But on that server, source seems to be included in sh
If it has source it is not strictly sh.
I had this type of problem on Jenkins (trying to source a file stored as "secret file") and sourcing by "." command was the solution.
For compatibility with Mac OS (dev environment) and Travis-CI (testing environment), I had to use . ./filename. Otherwise, I would get a sh: 1: .: filename: not found error when running tests on Travis-CI.
Thank you that worked on ubuntu 20.04. While I had to run shell script in npm command
|
73
$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

4 Comments

Nice! I am running RHEL and Ubuntu servers and I always have small issues such as this one with Ubuntu. I really like RHEL and RHEL like linux.
The accepted answer doesn't work on Ubuntu 14, this one does!
Please add some further explanation to your answer - what does it do? dpkg-reconfigure might not be accessible for all users, especially on systems that are not based on Debian
Fixed my source: not found errors with old telnet clients after debian upgrade, thanks.
60

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

Comments

19

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

1 Comment

The given question is not related to Jenkins after all
14

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(.)

Comments

10

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

2 Comments

Well, it says #!/bin/sh and on another linux if I manually enter sh and ask "which source" it tells me: source: shell built-in command
oh so this is what this line was for. I always wondered. huge thanks!
2

In case you cannot change the script to use "." instead of "source", change the link of "sh" to point to "bash" instead of "dash":

# which sh
/usr/bin/sh

# which bash
/usr/bin/bash

# ls -la /usr/bin/sh
lrwxrwxrwx 1 root root 4 Oct  5 15:55 /usr/bin/sh -> dash

# ln -sf /usr/bin/bash /usr/bin/sh

# ls -la /usr/bin/sh
lrwxrwxrwx 1 root root 4 Feb  6 09:18 /usr/bin/sh -> bash

Comments

1

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

sudo -s
source ./filename.sh

1 Comment

This does not look like a valid solution. What if the user does not have sudo permissions? And the sourced stuff is afterwards only available as admin user, which will cause new trouble
1

You have 2 options, switch from pure shell to bash which enables the bash features including source. https://superuser.com/questions/1220159/bash-prompt-variables-not-working/1764353#1764353

Or use . instead of source with this form

. ~/.your_file_name

for example

. ~/.bashrc

please consider one space between dot and tilde. Tilde (~) refers to the home directory, if your file is located somewhere above the home, use an absolute address from /

Comments

0

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

1 Comment

This has already been answered multiple times. Please provide more information if you add a new answer in such a case
0

I had this same issue recently and solved it by changing the shebang from #!/bin/bash to #!/usr/bin/env bash

Comments

0

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 StackExchange.

Comments

-3

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

Comments

-10

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>

1 Comment

The post says sh: 1: source: not found not file not found

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.