Short answer: source is a command built into bash and zsh, not a program, and the POSIX shell sh (dash on Debian and Ubuntu) does not have it. Its portable name is a single dot: write . ./file instead of source file, or run the script with bash.
. ./venv/bin/activate # works in sh, dash, bash and zsh
bash ./script.sh # or run the script with bash, not sh
# or make this the first line of the script (nothing after it on that line):
#!/usr/bin/env bashThe message comes in a few shapes, all with the same cause: sh: 1: source: not found when you type sh -c, /bin/sh: 1: source: not found from a Dockerfile RUN line or a Makefile, and script.sh: 2: source: not found when a script started with sh reaches line 2. Further down: a sourced file that cannot be found, and errors inside ~/.bashrc or ~/.zshrc.
Why sh says source: not found
source has to run inside the shell you are using, because its whole job is to change that shell: set variables, define functions, switch on a virtual environment. So it is a builtin, and builtins exist only in shells that implement them. The GNU bash manual lists source as a synonym for .. The POSIX standard defines only . (dot) as a special builtin and names source among the command names whose results are unspecified. dash follows the standard, and since Debian 6 (squeeze) it is the shell behind /bin/sh, per the Debian wiki. On our Debian 12 server:
$ readlink -f /bin/sh
/usr/bin/dash
$ sh -c 'source /dev/null'; echo $?
sh: 1: source: not found
127
$ bash -c 'source /dev/null; echo ok'
ok
$ bash -c 'type source .'
source is a shell builtin
. is a shell builtin
$ sh -c 'type source .'
source: not found
. is a special shell builtinExit status 127 is the shell's code for a command it could not find, explained in exit code 127. No package installs it, and sudo source fails too: sudo and env can only start programs, so env source /dev/null answers env: ‘source’: No such file or directory. To source something as root, start a root shell that does it: sudo bash -c '. ./file && command'.
| Shell | source | . (dot) |
|---|---|---|
| bash (also bash as /bin/sh on Fedora, Rocky Linux, AlmaLinux) | yes | yes |
| zsh | yes | yes |
| dash (/bin/sh on Debian and Ubuntu) | no: source: not found | yes |
| BusyBox ash (/bin/sh on Alpine) | yes, in Alpine's build | yes |
Two details explain why a script works on one machine and fails on the next. bash run as sh or with --posix still accepts source, so a script with a #!/bin/sh line that sources files runs fine on Fedora or Rocky and breaks on Debian. And Alpine's BusyBox is built with ASH_BASH_COMPAT (per the busybox config in Alpine's aports), which adds source to its ash, so the same Dockerfile can build on Alpine and fail on a Debian based image.
Where it breaks: every place that runs /bin/sh
These are the places that pick /bin/sh for you:
| Where | Why it is sh | Fix |
|---|---|---|
sh script.sh | You asked for sh; the shebang is ignored | bash script.sh |
#!/bin/sh | The shebang names sh | #!/usr/bin/env bash |
| Dockerfile RUN (shell form) | Docker runs ["/bin/sh", "-c"] on Linux | SHELL ["/bin/bash", "-c"] |
| Makefile recipe | GNU make uses /bin/sh unless SHELL is set in the makefile | SHELL := /bin/bash |
| cron job | cron sets SHELL=/bin/sh | SHELL=/bin/bash |
GitHub Actions shell: sh | The step runs sh -e {0} | shell: bash |
sudo, env, xargs, find -exec | They start programs, and source is not one | bash -c '. ./file; cmd' |
Fix a script: use the dot, or run it with bash
If the script is meant to be portable, keep #!/bin/sh and replace every source file with . ./file. The ./ matters: when the name has no slash, dot searches only PATH, as POSIX requires (see the next section). If the script uses other bash features anyway (arrays, [[ ]]), change the first line to #!/usr/bin/env bash and start it with ./script.sh or bash script.sh, never sh script.sh. Our guide on how to run a .sh file compares the four ways to start a script.
$ cat run.sh
#!/bin/sh
source ./vars.sh
echo "$GREETING"
$ sh run.sh
run.sh: 2: source: not found
$ bash run.sh
hello
$ sed -i 's/^source /. /' run.sh && sh run.sh
helloDockerfile: /bin/sh: 1: source: not found
The shell form of RUN (a plain command, not a JSON array) runs through /bin/sh -c, which the Dockerfile reference gives as the Linux default. In a Debian or Ubuntu based image, such as the official python images, that is dash. Three fixes, best first:
# 1. Do not activate at all: call the venv's own programs, or put them on PATH
RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
RUN pip install -r requirements.txt
# 2. Use the dot in the same RUN line as the command that needs it
RUN . /opt/venv/bin/activate && pip install -r requirements.txt
# 3. Switch the shell for all later RUN, CMD and ENTRYPOINT shell forms
SHELL ["/bin/bash", "-c"]
RUN source /opt/venv/bin/activate && pip install -r requirements.txtEvery RUN line starts a new shell, so RUN . venv/bin/activate on a line of its own does nothing for the next line. The Python venv documentation says you do not need to activate a virtual environment at all if you call its interpreter by full path, which is why option 1 is the cleanest. The activate script itself is fine under dash; only the word source is not:
$ /bin/sh -c 'source venv/bin/activate'
/bin/sh: 1: source: not found
$ sh -c '. venv/bin/activate && command -v python'
/srv/app/venv/bin/pythonIf you hit externally-managed-environment while setting up that venv, see pip: command not found, which covers creating one on Debian and Ubuntu.
Makefile, cron and CI jobs
Makefile
The GNU make manual says that if SHELL is not set in your makefile, /bin/sh is used, and that SHELL is never taken from your environment. Your login shell being bash or zsh changes nothing. Set it in the makefile:
$ make
source ./vars.sh && echo "$GREETING"
/bin/sh: 1: source: not found
make: *** [Makefile:2: all] Error 127
$ sed -i '1i SHELL := /bin/bash' Makefile && make
source ./vars.sh && echo "$GREETING"
hellocron
crontab(5) on Debian says cron sets SHELL to /bin/sh, and that a crontab may override it. Put SHELL=/bin/bash on a line above the jobs, or call the job as bash /path/to/script.sh. On a VPS this is the classic case of a script that works in your SSH session and fails at night: your session is bash, the cron job is dash. More on the cron environment is in the crontab guide.
GitHub Actions and other CI
On Linux runners a step without shell: runs bash -e {0}, and only shell: sh (or a container without bash) gives you sh -e {0}, per GitHub's workflow syntax reference. Note that shell: bash runs with --noprofile --norc, so ~/.bashrc is not read for you; source what you need in the step itself.
source: No such file or directory
Once the shell knows the command, the next error is usually the path. Both shells look up a name without a slash in PATH; bash then tries the current directory as well, dash does not (and neither does bash in POSIX mode). dash also stops the script when the file is missing, as POSIX requires; bash prints the error and carries on:
$ bash -c 'source vars.sh; echo $GREETING'
hello
$ sh -c '. vars.sh'
sh: 1: .: vars.sh: not found
$ bash --posix -c 'source vars.sh'
bash: line 1: source: vars.sh: file not found
$ sh -c '. ./nofile; echo after'
sh: 1: .: cannot open ./nofile: No such file
$ bash -c 'source ./nofile; echo after'
bash: line 1: ./nofile: No such file or directory
after- Use
./or an absolute path. A relative path is relative to the directory you run from, not to the script. Inside a bash script,source "$(dirname "$0")/vars.sh"finds a file next to the script wherever it is started from. - A venv that is not there.
source venv/bin/activatefails this way when the venv was created under another name (.venv) or in another directory.find . -maxdepth 3 -path '*/bin/activate'shows what exists.
command not found inside ~/.bashrc or ~/.zshrc
When source ~/.bashrc (or a new terminal) prints an error, the problem is not source but a line in the file. The message names the file and the line, so read it from left to right. A throwaway file whose line 3 calls a tool that is not installed:
$ bash -c 'source ./fake_bashrc'
./fake_bashrc: line 3: nvmx: command not found
$ sh -c '. ./fake_bashrc'
sh: 3: ./fake_bashrc: nvmx: not found
$ bash -c 'source ./crlf.sh'
./crlf.sh: line 2: $'\r': command not found- A tool you removed or never installed. Lines such as
eval "$(tool init -)"left behind by an installer fail once the tool is gone. Delete the line or guard it:command -v tool >/dev/null && eval "$(tool init -)". The rest of the file still runs, as theFOO=1after the failing line showed in our test. $'\r': command not foundmeans the file has Windows line endings.sed -i 's/\r$//' fileconverts it.- A broken
PATHline. If every command fails after sourcing, anexport PATH=line probably lost$PATH. The right form is in how to set environment variables on Linux.
zsh also names the file and line, in its own format: /home/you/.zshrc:3: command not found: nvmx (per zsh's Src/exec.c and Src/utils.c). To see which line does what in bash, trace it: bash -x -c 'source ~/.bashrc' prints every command before it runs, and bash -n ~/.bashrc checks the syntax without running anything.
Confirm the fix
readlink -f /bin/sh # dash means source will not work there
head -1 script.sh # which shell the shebang asks for
grep -n '^ *source ' script.sh # lines to turn into ". ./file" for sh
sh -n script.sh && sh script.shFor cron, Docker and make, test the command the way the system runs it, /bin/sh -c '...', before you rely on it.
Frequently asked
Why does sh say source: not found?
source is a builtin of bash and zsh, not a program, and POSIX sh only defines the dot command. On Debian and Ubuntu /bin/sh is dash, which has no source. Write . ./file instead, or run the script with bash.
How do I fix /bin/sh: 1: source: not found in a Dockerfile?
The shell form of RUN uses /bin/sh -c on Linux. Use . /opt/venv/bin/activate && your-command in the same RUN line, add SHELL ["/bin/bash", "-c"] above it, or skip activation and put the venv on PATH with ENV PATH="/opt/venv/bin:$PATH".
Why does source work in my terminal but not in cron or make?
Your terminal runs bash or zsh, but cron sets SHELL to /bin/sh and GNU make uses /bin/sh unless the makefile sets SHELL. Add SHELL=/bin/bash to the crontab or SHELL := /bin/bash to the makefile, or use the dot command.
What does command not found in .bashrc or .zshrc mean after source?
source worked, but a line in the file runs a command that does not exist. The message gives the file and line number, for example /home/you/.bashrc: line 3: tool: command not found in bash or /home/you/.zshrc:3: command not found: tool in zsh. Remove or guard that line; $'\r': command not found means the file has Windows line endings.