230

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?

Milad
  • 4,134
  • 5
  • 29
  • 39

12 Answers12

241

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

choroba
  • 216,930
  • 22
  • 195
  • 267
  • 3
    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 Dec 04 '12 at 12:51
  • 1
    @Milad: On recent Ubuntus, `/bin/sh` calls `/bin/dash`. Traditionally, `/bin/sh` called `/bin/bash` is sh-compatibility mode. – choroba Dec 04 '12 at 13:03
  • @Milad I think it does not work on ubuntu 14.04. What is your OS version? – Reza Ameri Dec 29 '14 at 19:32
  • 1
    Maybe help known - if you have cascade scripts then rename all "sh -> bash". Thank you. :-) – BGBRUNO May 12 '16 at 00:48
  • 3
    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. – fIwJlxSzApHEZIl Jun 11 '19 at 15:42
188

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)

Hawkeye Parker
  • 6,680
  • 7
  • 40
  • 44
Guru
  • 15,464
  • 2
  • 31
  • 43
63
$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

gturri
  • 12,179
  • 9
  • 39
  • 55
shlsy
  • 647
  • 5
  • 2
  • 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. – radtek Mar 14 '14 at 19:05
  • The accepted answer doesn't work on Ubuntu 14, this one does! – rohithpr Oct 20 '15 at 08:50
  • 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 – Nico Haase May 15 '20 at 15:29
  • Fixed my `source: not found` errors with old telnet clients after debian upgrade, thanks. – bjoster May 16 '20 at 14:30
47

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

Travis Clarke
  • 5,065
  • 5
  • 27
  • 36
14

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
Mojtaba Yousefi
  • 588
  • 1
  • 8
  • 25
10

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

GangaRam Dewasi
  • 513
  • 4
  • 11
9

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
mah
  • 38,251
  • 9
  • 74
  • 91
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 stack exchange.

Gord Wait
  • 153
  • 1
  • 9
0

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

sudo -s source ./filename.sh

swati jain
  • 17
  • 1
  • 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 – Nico Haase May 15 '20 at 15:27
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
DInfo2019
  • 11
  • 1
  • 1
    This has already been answered multiple times. Please provide more information if you add a new answer in such a case – Nico Haase May 15 '20 at 15:28
-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

Kellen Stuart
  • 6,353
  • 5
  • 50
  • 73
-8

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>
mosu
  • 11
  • 1