What do you do with Environment Variables? - shell

When I execute environment commands, such as env, set, and unset, something happens, but what?
set hello='hello world!'
unset find
What do you do with the commands? Are the changes permanent or temporary? Where can you see the changes? I am an Ubuntu-newbie.

The changes are temporary. They persist only in the current shell. When you set an environment variable in your ~/.profile or ~/.bash_profile (just use the one that already exists, use ls -a ~ to see), they will be effectively permanent, as these files are "sourced" every time you open up a new shell.
For instance, if you added:
export HELLO="world"
To your ~/.profile, that variable would become available every time you open a new shell (you can refresh your current shell with source ~/.profile). You could test it with:
$ echo $HELLO
world
Environment variables are used for scripts all over your system. You can do things like set your favourite editor, e.g.:
export EDITOR="nano"
One useful thing you can do is set your prompt string, e.g.:
Bill:~$ export PS1="\u is awesome$ "
Bill is awesome$ ls
Bill is awesome$ du -h
Bill is awesome$ ...etc...

Each process that is created gets its own environment which lives as long as the process. Your shell is just like any other process. Its environment is its own.
If you type 'set' with no arguments, you'll see what exists. Many of these settings are there to control program behavior.. your search path, desired X11 display, home directory (if not /home/yourname), etc.
The use is really whatever you need it to be. Any time you need to store some useful bit of information (such as long list of command line options to some program) into a variable that other applications can read, or that you can access from the shell, use the environment.
For instance:
USUAL_CONFIGURE_OPTS="--prefix=/home/charlie --sysconfdir=/home/charlie/tmp-etc"
./configure $USUAL_CONFIGURE_OPTS --and-additional-arguments
Edit:
As a programmer, I read the environment to determine the user's preferences and obey them. For instance, the variable POSIXLY_CORRECT influences the output of my programs if it is set. The environment is where the user tells programs how to behave. It also happens, the environment is a handy place for the user to store useful bits, as I described above.
Again (responding to your comment), every program that is executed is a process. A process gets its own address space (own memory), its environment is stored in that space. This means, the environment is specific to that process and lives only as long as the process itself.
Edit 2:
I think that I now fully understand your question. If someone says 'virtual environment', they are just noting that the environment resides in the application's address space, which is mapped by the kernel as virtual memory (some pages might be in physical memory, some might be in swap, shared dynamic objects, etc).
No process can access another's environment unless the process explicitly creates a map to that specific region and shares it with another process. Again, a process' address space is completely private and isolated from other processes. Environmental variables live within that address space, otherwise, the process could not access or manipulate them.

Just as with the argument list, there is a section of memory allocated for the process that stores environment variables. I believe it is a \0 separated, unsorted list of KEY=VALUE pairs.
This bit of memory is copied with every fork() and not erased by exec() so any changes are copied into child processes.
The Unix shells do not write variables into this environment variable memory until export is used. That is the difference between shell variables and environment variables.
In the bash shell, "x=2" does not set an environment variable. "export x=2" or "x=2; export x" does.

Related

set env variable in bash for t seconds

Is there a way to set env variable for certain number of seconds and then reset. I have a script that reads the env variable. If it is set, script should exit. Basically I am trying to build a snooze algorithm. eg. It keeps on sending messages as it receives it. However sometimes I need to snooze them for say t seconds, during these t seconds it would buffer all incoming messages and when timer t expires, send the buffer contents. My approach is to use an
if [ $MY_ENV=set ]
then
buffer
fi
However this env variable must not be shell specific. It should be for every user logged in from different locations on the same machine.
Regardless of the timing part, there's no way to set a variable in the environment of an already-running process from outside that process.
You need to use the file system for this. If it's user-specific, it can be a file in the user's home directory, for example.
If it shouldn't be shell specific, I'd use some kind of lock file for this purpose. If the lock file exists, that's the equivalent of your var being set. You can have something scheduled to delete it after t seconds.
Look at the lockfile command.

Can variables created in one batch file be used in another?

I have some batch files I'm trying to organize better, generally trying to get it to a spot where they might be able to be worked on a little easier by someone who comes down the road eventually.
In doing this it occurs to me that I don't know if batch scripting will persist variables in memory. Here's a example of what I have:
SET SQLSERVER="TestServer1"
SET SQLTEXT="select * from some_table"
IF NOT %SQLSERVER% = ""
CALL RUNSQL.BAT
END IF
In RUNSQL.BAT I have something similar to this:
SET SQLCMD="C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE"
%SQLCMD% -S %SQLSERVER% -Q %SQLTEXT"
EXIT
My question is: Will the variables set in that first bit of text persist into RUNSQL.BAT?
Environment variables are stored inside a block of memory in the memory space of each process. Each time a process is started, the child process gets a copy of the environment of the parent process, so at the time of start both see the same data, but don't share the same variables, child has a COPY. Any change in the environment of the parent is not seen in the environment of the child. Any change in the environment of the child is not seen in the environment of the parent.
In batch files, if both scripts are running inside the same cmd instance, both share THE SAME VARIABLES, those in the environment memory of the cmd.exe process that is running both batch files.
In the posted sample code, cmd.exe is running the first batch script, and it, inside the same cmd, calls the second batch script, that is running inside the same cmd instance, so, both are sharing the same environment variables. Second batch sees the same variables and any change made in second batch is seen in first on return of the call.

Does echo %PATH% expand to only the system or also the user variables?

In the top voted comment of the best answer here (How to persistently set a variable in Windows 7 from a batch file?) it is mentioned that %PATH% expands to the system plus the user variables. Is that true? In my laptop if I open cmd and I digit
echo %PATH%
I only see the system variables. Can anybody clarify this point?
cheers
A.
The environment that is, by default, given to a new process, merges both system and user variables. So, if you have a PATH variable defined for both system and user then the two values are merged.
Your experiment appears to contradict my statement above. However, when I repeat your experiment I can see values from both system and user settings merged. So I can only conclude that you are not performing your experiment correctly.
Update: Perhaps this comment from the MSDN topic on environment variables might be pertinent:
Found out that on Windows Server 2003, once the system PATH passes 1920 characters, the user PATH environment variable is no longer merged with it to set the process PATH environment variable, even though the full system PATH (even if larger) will be included in the process PATH variable.

How to export a shell variable to all sessions?

I would like to know is there a way to export my shell variable to all sessions in the system (not only the current session). I'm not looking to set it in .bashrc file as the shell variable is a dynamic one it changes time to time.
You can set up your sessions to keep rereading a file on disk by setting a trap on DEBUG in your .bashrc:
trap 'source ~/.myvars' DEBUG
If you leave a terminal A open, run echo VAR=42 >> ~/.myvars in terminal B, then switch back to terminal A and echo $VAR, it'll "magically" be set.
You seem to misunderstand what export does. All it does is to move a local variable into the environment block within the process (/proc/$$/environ).
When a new process is created (a fork) then the program data areas, including the environment block, are copied to the new process (actually they are initially shared, then copied when one writes). When a different program is run (execve), by default the environment block remains from the previous program. See also the env(1) program.
So environment variables are normally inherited (copied) from their parent process. The only way to get a new environmnt variable into a running process is to use some sort of inoculation technique, as a debugger would do. Writing such a program is not an easy task, and I'm sure you could imagine the security implications.
You can't. A better explanation can be found in the unix stackexchange section here!
A shell variable probably is not suited for the use you are trying to achieve. Maybe you want to use files instead.

Current value of process' environment variable

I was wondering if there is a way to set an environment variable from a bash process and read it from another.
As environment variables' values are local to processes (besides the inheritance), one can't just do export FOO="bar" in a terminal and read it from another.
I was then trying to get them through /proc/environ, but this is what I got:
etuardu#subranu:~$ FOO="foo" bash
etuardu#subranu:~$ strings /proc/$$/environ | grep FOO
FOO=foo
etuardu#subranu:~$ export FOO="bar"
etuardu#subranu:~$ strings /proc/$$/environ | grep FOO
FOO=foo
etuardu#subranu:~$ echo $FOO
bar
It seems I can just get the value that that environment variable had when the process started.
How about its current value?
This is not possible in general because of the way environment variables work.
When a process is first execed, the kernel supplies it with its initial set of environment variables together with some other stuff (mainly its argv vector, i.e. its command line). Thereafter, this list (like the argv vector) is just a regular C array of character pointers inside the process. The process is free to manage them however it likes, including completely recycling the memory that holds the strings for some other use if it wants to. It isn't safe for one process to go peeking inside any other process's memory space to find its environment variables.
Most types of processes do use the list of environment variables supplied by the kernel more or less as-is, perhaps querying it and modifying it with C library functions like getenv() and putenv(). If these processes run any other executables in their turn, they pass the same environment vector to the execve system call which they received at the start of their own execution, which means that the new executable gets the same environment (possibly enhanced by some calls to putenv()).
Shells are another matter. Because environment variables are so important in shell scripts, some shells use the environment vector supplied to them only as a "start value", and thereafter ignore it. They manage their environment variables using their own, more capable, data structures. When they execute child processes of their own, they pass a completely new environment vector to the new process, constructed from those internal data structures. This means that even if you were to peek into a shell's memory space to find its environment variables as suggested above, you would find only the initial set, not the environment that the shell is actually using!
What you might be able to do is query a process's initial set of environment variables, that is, the same vector that was passed to the executable when it started. But this is not portable, and there is considerable variation even among operating systems that support this. For example, in historical UNIXes, it is still a bit ugly because it still involves peeking into the process's memory space (albeit a special area of memory called the "user area"). Linux makes this possible more elegantly: the vector can be found as a text string in /proc/<pid>/environ. On most systems where it's possible to get this information, ps has an e option which is able to show it.

Resources