Milad
Milad

Reputation: 5510

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?

Upvotes: 326

Views: 561820

Answers (15)

Shayne T. Thiessen
Shayne T. Thiessen

Reputation: 57

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

Upvotes: 0

Daniel Andrzejewski
Daniel Andrzejewski

Reputation: 686

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

Upvotes: 2

for development
for development

Reputation: 41

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 Tilda. Tilda refers to the home directory, if your file is located somewhere above the home, use an absolute address from /

Upvotes: 1

Guru
Guru

Reputation: 16984

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)

Upvotes: 256

DInfo2019
DInfo2019

Reputation: 11

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

Upvotes: 0

GangaRam Dewasi
GangaRam Dewasi

Reputation: 741

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

Upvotes: 14

choroba
choroba

Reputation: 241868

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

Upvotes: 345

swati jain
swati jain

Reputation: 35

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

sudo -s source ./filename.sh

Upvotes: 1

Travis Clarke
Travis Clarke

Reputation: 6671

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

Upvotes: 56

Kellen Stuart
Kellen Stuart

Reputation: 8893

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

Upvotes: -3

Mojtaba Yousefi
Mojtaba Yousefi

Reputation: 676

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

Upvotes: 20

Gord Wait
Gord Wait

Reputation: 152

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.

Upvotes: 0

mosu
mosu

Reputation: 7

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>

Upvotes: -10

shlsy
shlsy

Reputation: 745

$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

Upvotes: 72

mah
mah

Reputation: 39807

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

Upvotes: 10

Related Questions