[sf-lug] printenv "vs." set [was: Re: shell, man(1) [was: ...

jim jim at well.com
Sun Aug 21 11:30:51 PDT 2016


     I've felt confused about the difference, too.
     I looked up one site that told me, essentially,
that the difference between a shell variable and an
environment variable is that the latter is exported
while the former is not.
     I think that's not enough. A shell variable is
one that the shell creates at the time the shell is
loaded. Different shells set different variables.

$ man bash
     Scroll down to the Shell Variables section. See
the  dash  man page and scroll down to the
ENVIRONMENT section, which has fewer itsms.

     The /etc/profile file does some initializing.
     The /etc/profile.d/ directory lists related
initialization files.
     My /etc/ directory has the bash.bashrc (the "rc"
part stands for "run commands")  as well as the b
ash_completion shell script and the bash_completion.d/
directory (that lists a lot of files).

     The bash man page defines "environment" as an
array of strings. I understand the environment is an
area of memory that stores lines of text in a
key=value format (along with function definitions).
     The  export  (built-in) command tags a variable
name that should be copied into the environment of any
child program. For example,

$ ls
this    that    more

     To load the ls program, the shell makes a copy
of itself, including the environment variables
marked for export, and then replaces the executable
code (the "text" segment) with the code for the  ls
program.

$ TRY=hard
$ echo $TRY
hard
$ export TRY
$ export
<bunch of stuff much of which is  declare -x ENVNAME=value>
$ ls
this    that    more
$

     The  ls  program above inherits all the declare -x
variables, including the  TRY  variable. The  ls  program
ignores pretty much all of the environment (see the man
page and/or source code for the  ls  program) then
terminates. The kernel replaces the  ls  program with
whatever program called it, the  bash  shell in this case.

     Is the TRY variable an environment variable (it's
exported)? A shell variable (I made it using the shell)?
My own, special variable in a class of its own?





On 08/21/2016 04:52 PM, Alex Kleider wrote:
> On 2016-08-21 08:49, Michael Paoli wrote:
>
>> Yes, they're not doing the same thing.
>> RFTM.  :-)
>>
>> bash(1)
>> ...
>>>> set [--abefhkmnptuvxBCEHPT] [-o option-name] [arg ...]
>>>> set [+abefhkmnptuvxBCEHPT] [+o option-name] [arg ...]
>>>>       Without options, the name and value of each shell variable
>>
>> printenv(1)
>> ...
>>        Print the values of the specified environment VARIABLE(s)
>> ...
>
> ...but TFMs suggest to me that without params, they should in fact be 
> doing the same thing.  (... unless "shell variable" is not the same as 
> "environment VARIABLE" in which case I've more to learn.)
>
> _______________________________________________
> sf-lug mailing list
> sf-lug at linuxmafia.com
> http://linuxmafia.com/mailman/listinfo/sf-lug
> Information about SF-LUG is at http://www.sf-lug.org/
>





More information about the sf-lug mailing list