I am attempting to work with an existing library of code but have encountered an issue. In short, I execute a shell script (let's call this one A
) whose first act is to call another script (B
). Script B
is in my current directory (a requirement of the program I'm using). The software's manual makes reference to bash
, however comments in A
suggest it was developed in ksh
. I've been operating in bash
so far.
Inside A
, the line to execute B
is simply:
. B
It uses the "dot space" syntax to call the program. It doesn't do anything unusual like sudo
.
When I call A
without dot space syntax, i.e.:
./A
it always errors saying it cannot find the file B
. I added pwd
, ls
, whoami
, echo $SHELL
, and echo $PATH
lines to A
to debug and confirmed that B
is in fact right there, the script is running with the same $SHELL
as I am at the command prompt, the script is the same user as I am, and the script has the same search path $PATH
as I do. I also verified if I do:
. B
at the command line, it works just fine. But, if I change the syntax inside A
to:
./B
instead, then A
executes successfully.
Similarly, if I execute A
with dot space syntax, then both . B
and ./B
work.
Summarizing:
./A
only works if A
contains ./B
syntax.
. A
works for A
with either ./B
or . B
syntax.
I understand that using dot space (i.e. . A
) syntax executes without forking to a subshell, but I don't see how this could result in the behavior I'm observing given that the file is clearly right there. Is there something I'm missing about the nuances of syntax or parent/child process workspaces? Magic?
UPDATE1: Added info indicating that the script may have been developed in ksh
, while I'm using bash
.
UPDATE2: Added checking to verify $PATH
is the same.
UPDATE3: The script says it was written for ksh
, but it is running in bash
. In response to Kenster's answer, I found that running bash -posix
then . B
fails at the command line. That indicates that the difference in environments between the command line and the script is that the latter is running bash
in a POSIX-compliant mode, whereas the command line is not. Looking a little closer, I see this in the bash
man
page:
When invoked as sh, bash enters posix mode after the startup files are read.
The shebang
for A
is indeed #!/bin/sh
.
In summary, when I run A
without dot space syntax, it's forking to its own subshell, which is in POSIX-compliant mode because the shebang
is #!/bin/sh
(instead of, e.g., #!/bin/bash
. This is the critical difference between the command line and script runtime environments that leads to A
being unable to find B
.