Sample Header Ad - 728x90

export command behaviour in bash vs bourne shell

1 vote
3 answers
339 views
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.
Asked by Ben Ldr (13 rep)
Mar 28, 2023, 07:48 AM
Last activity: Mar 29, 2023, 07:11 AM