bash v3.2 (though I think holds for newer versions too):
In section 3.7.4 Environment, the docs say:
On invocation, the shell scans its own environment and creates a parameter for each name found, automatically marking it for export to child processes.
And later in Appendix B Major Differences From The Bourne Shell, the docs say:
Variables present in the shell’s initial environment are automatically exported to child processes. The Bourne shell does not normally do this unless the variables are explicitly marked using the export command.
I don't understand what this means.
In the following, cmd1.sh
comprises
#!/bin/bash
echo yes $ben from cmd1
./cmd2.sh
And cmd2.sh
comprises
#!/bin/bash
echo yes $ben from cmd2
I first understood the docs to mean that all assigned variables will be exported (ie there was no need to export
variables), ie when running
ben=you;
./cmd1.sh
I expected this to print
yes you from cmd1
yes you from cmd2
But instead it prints
yes from cmd1
yes from cmd2
So variable ben
doesn't appear to be automatically exported. Then I thought the docs might instead mean that all environment variables will be exported, ie when running
ben=you;
export ben;
./cmd1.sh
Because cmd1 receives an environment variable of ben
, then ben
will be automatically exported such that it will be visible in cmd2. Ie I expected this to print the following (and indeed the following is printed):
yes you from cmd1
yes you from cmd2
However, to test whether this is different from Bourne shell (as the docs claimed) I ran the exact same commands, but changing the shebang to point to /bin/sh
instead of /bin/bash
, and I obtained the exact same result. Ie I did not see any difference. In Bourne shell, I was expecting to see an output of something like
yes you from cmd1
yes from cmd2
Can anyone help me to understand what the docs are referring to when they talk of "automatically" marking parameters for export, and how this is different to Bourne shell?
Nb I did spot this question regarding a specific difference between export
behaviour in bash and bourne, but that doesn't seem to be relevant.
ls -la /bin/sh
)/bin/sh
is the Bourne shell. What operating system are you using? The Bourne shell is not really seen in the wild anymore.ls -la /bin/sh
gave-rwxr-xr-x 1 root wheel 150384 24 Aug 2022 /bin/sh
. I'm running macOS Monterey, version 12.6. Ie a modern Mac.../bin/sh --version
. Just note that when called assh
, Bash goes to its POSIX mode, where it acts slightly different from when called asbash
.../bin/sh --version
shows it's Bash 3.2.