Does the dot command behave uniformly across different shells?

123 Views Asked by At

You can use the . command to execute commands from a shell script as if they were in the calling shell script itself.

If I use the dot command in my shell scripts, will it behave similarly across different shell interpreters?

PLEASE Note: I'm not asking about the difference between the . command and the source command. If that's what you want to learn, please see this other question: Using dot or "source" while calling another script - what is the difference?

1

There are 1 best solutions below

4
Elifarley On

Under certain simple shells, the dot operator doesn't behave as expected:

$ /bin/sh --help
BusyBox v1.32.1 () multi-call binary.

$ cat /tmp/dot-sh.sh
#!/bin/sh
. xxx || echo "dot worked as expected"

$ /tmp/dot-sh.sh
/tmp/dot-sh.sh: 2: .: xxx: not found

To test the example above, you can:

docker run --rm -it alpine sh

But under /bin/bash, the dot operator does behave as expected:

$ cat /tmp/dot-bash.sh
#!/bin/bash
. xxx || echo "dot worked as expected"

$ /tmp/dot-bash.sh
/tmp/dot-bash.sh: line 2: xxx: No such file or directory
dot worked as expected