0

I'm trying to source (bash-parlance) a file in dash using . file as specified by the man page:

 . file
        The commands in the specified file are read and executed by the
        shell.

But I get an error even though the file is there:

$ ls
defaults.sh  run.sh
$ cat run.sh 
#!/bin/sh

. defaults.sh

echo "VAR: $VAR" $ cat defaults.sh VAR=abc $ bash run.sh VAR: abc $ dash run.sh run.sh: 3: .: defaults.sh: not found

The same thing happens when I try . defaults.sh in an interactive dash.

Where am I going wrong here?

2 Answers2

2

For some shells, you need to put ./ or an absolute path in front:

. ./defaults.sh

Note, that you might want to add the absolute path of the run.sh script, otherwise defaults.sh will be sourced from the user's location:

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
. "$SCRIPT_DIR"/defaults.sh
pLumo
  • 22,565
1

Attempt to add $PWD to your PATH if running "dash run.sh" from the directory where the run.sh and defaults.sh file exists.

In my test I saw these results:

# dash run.sh
run.sh: 3: .: defaults.sh: not found

I then took this step:

# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# export PATH=$PATH:$PWD

Results after added $PWD to the PATH:

# dash run.sh
VAR: abc
DavidW
  • 11
  • 1
    Changing PATH to source a file is not a very good idea I think but might be a last resort if you have a script you cannot change. – pLumo Apr 27 '22 at 08:25
  • @pLumo Thnak you for that information. I will keep that information and the solution you provided in my knowledge base as I'm sure I'll see this problem again soon. – DavidW Apr 29 '22 at 09:30