Tanguy,
Shell variables are most often used to pass default settings to applications.
These shell variables are collectively known as the environment.
When any application is executed (including a shell), it is passed three
pieces of information, the argument count, the argument array, and the
environment array. See the coding example below for a demonstration.
Microsoft Windows NT has recently implemented a similar system known as
the registry.
An almost universal usage has evolved over the decades for many shell
variables.
"The shell has variables, like those in most programming languages, ...
The value of a variable is associated with the shell that creates it, and
is not automatically passed to the shell's children. ...
When you want to make the value of a variable accessible in sub-shells, the
shell's export command should be used."
-- The Unix Programming Environment, by Brian W. Kernighan and Rob Pike,
p.88-91, 1984 Bell Telepone Laboratories Inc., ISBN 0-13-937699-2,
LC 83-62851
Note: csh, a popular shell, is the exception that exports all variables by
default.
In Borne shell, Korn shell, and Bash, a shell variable is set (and created
if necessary) by typing at a prompt (or in a shell script) '<name>=<value>'
without spaces around the '='. The shell variable can be exported using
'export <name>'. In Korn shell and Bash, a variable can be exported and
set simultaneously with 'export <name>=<value>'.
In csh, shell variables are set using 'set <name> = <value>' and are
exported by default.
In Borne shell, Korn shell, Bash, and csh, shell variables are destroyed
by exiting the shell or by using 'unset <name>'.
Shell variables have dynamic scope. A shell variable exists and its value
is readable from the time it is created until the shell exits or the
variable is destroyed. It is readable by the shell (and if exported) by
any of its descendants.
When any application is executed (including a shell), it is passed three
pieces of information, the argument count, the argument array, and the
environment array. See the coding example below for a demonstration. The
last element in the environment array is always (char const *) 0.
The documentation for most applications describes the shell variables that
they use. An almost universal usage has evolved over the decades for many
shell variables. Most systems set the following variables at login using a
system-wide script (e.g. /etc/profile), and users optionally reset all or
some in shell configuration files (e.g. $HOME/.bashrc, $HOME/.cshrc,
$HOME/.profile).
HOME=<directory>
The directory for information peculiar to the user.
csh, Bash, and Korn shell substitute this for ~ in command lines.
SHELL=<executable>
The executable used for interactive commands.
PAGER=<executable>
The executable used to present information to a screen user.
EDITOR=<executable>
The executable used to interactively edit a file.
PATH=<directory>:<directory>:<directory>...
The list of directories to search for an application's executable.
LD_LIBRARY_PATH=<directory>:<directory>:<directory>...
The list of directories to search for dynamic libraries.
INFOPATH=<directory>:<directory>:<directory>...
The list of directories to search for online interactive help.
MANPATH=<directory>:<directory>:<directory>...
The list of directories to search for online manuals.
WASTEBASKET=<directory>
The directory used for life limited (e.g. deleted, temporary backup) files.
MAIL=<file>
The file to which incoming mail is delivered.
LPDEST=<printer-id>
The printer used.
TERM=<terminal-type>
Identifier used for the current screen capabilities.
TZ=<timezone-identifier>
Identifier used for the current time and date conventions.
-- coding example: main.c --
/*
* Project: C Tutorial
* File: Unix Calling Convention Demonstration
* Author: Dr. Robert J. Meier
* History: 94-04-29 -rjm- file creation
*/
/*
* Constants
*/
/*
* Includes
*/
#include <stdio.h> /* printf() */
/*
* Support Routines
*/
/*
* Main program
*/
int main(int argc, /* argument count */
char *const *argv, /* argument array */
char *const *envp /* environment array */
) {
/* Print each argument */
printf("Arguments\n"); while (0 < argc--) printf(" '%s'\n", *argv++);
/* Print each shell variable */
printf("Environment\n"); while (*envp) printf(" '%s'\n", *envp++);
/* Report success */
return 0;
}
-- coding example: main.c --
-- coding example: main.C --
// -*- mode: c++ -*-
// Project: C++ Tutorial
// File: Unix Calling Convention Demonstration
// Author: Dr. Robert J. Meier
// History: 94-04-29 -rjm- file creation
//
// Constants
//
//
// Includes
//
#include <iostream.h> // cout
//
// Support Routines
//
//
// Main program
//
int main(int argc, // argument count
char *const *argv, // argument array
char *const *envp // environment array
) {
// Print each argument
cout << "Arguments" << endl;
for (; 0 < argc; argc--, argv++) cout << " '" << *argv << "'" << endl;
// Print each shell variable
cout << "Environment" << endl;
for (; *envp; envp++) cout << " '" << *envp << "'" << endl;
// Report success
return 0;
}
-- coding example: main.C --
Please email me directly if you need further assistance.
Hopefully helpful,
--
Dr. Robert J. Meier
Nowhere man(1)
He's a real(3) UNIX man(1)
Sitting in his UNIX LAN
Making all his UNIX plans
For nobody.
Knows the blocksize from du(1)
Cares not where /dev/null goes to
Isn't he a bit like you
and(3) me?
UNIX man(1), please listen(2)
My login(1) is missin'
UNIX man(1)
The wo-o-o-orld is at(1) your command.
He's as(1) wise as(1) he can be
Uses bash(1) and(3) perl(1) and(3) C
UNIX man(1), can you help(1) me at(1) all?
UNIX man(1), don't worry
test(1) with time(1), don't hurry
UNIX man(1)
The new kernel boots, just like you had planned.
He's a real(3) UNIX man(1)
Sitting in his UNIX LAN
Making all his UNIX plans For nobody ...
Making all his UNIX plans For nobody.
-- music by the Beatles, words by unknown
FANUC Robotics North America, Inc. [EMAIL PROTECTED]
Voice: 1-248-377-7469 Fax: 1-248-377-7363