Tải bản đầy đủ (.docx) (11 trang)

Shell Variables

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (106.9 KB, 11 trang )

Shell Variables
Objectives
Upon completion of this module, you should be able to:
• Set and unset shell and environment variables for the shell
• Change the PATH environment variable for the shell
• Use the which command to determine which version of a command is
being used
• Use the whereis command to search for instances of a command on a
system
Discussion – How would it be useful to customize shell?
Introduction
A variable is a placeholder for information to be used by the system or user.
Information such as the default printer or a pathname to a directory can be set
up as a variable.
Two categories of variables are discussed in the following section:
• Local (shell)
• Global (environment)
Two key environment variables are the PATH and ENV variables.
This module covers setting and unsetting shell and environment variables.
Although there are many shells : Bourne shell, Korn shell, C shell, Bourne Again
shell…,this module focuses on bash shell (Bourne Again SHell ).
Shell Variables
Overview
When you first log in to the host, you are placed in a predefined shell. If
you type sh (Bourne shell), ksh (Korn shell), or csh (C shell) on the
command line, a subshell is created. This process can be repeated to
create additional shells. To change to the previous shell, type exit.
Shell variables (local and global) can be either user-defined or built-in,
and can be customized by the user or predefined by the system.
Initially, when a variable is created, it is only available to its shell of
origin. This is a local variable. If a new subshell is created, the variables


created in the parent shell are not available. However, the parent shell is
still running, and when the subshell is exited, the variables will be
available again. When the shell where the variables were created is
exited, the variables of that shell are terminated. Local
variables are available only to the specific shell where they are created.
To make a local variable available in all subshells, it must be exported,
either by adding it to an initialization file, as discussed in Module 11,
‘‘Initialization Files,” or by exporting it on the command line.
Note – See the man pages on bash for variable definitions.
Local Shell Variables
A user-defined variable enables you to determine both the name of the
variable and its value. For example, a pathname could be assigned for
the on-line dictionary used in text editing programs.
By convention, shells use capital letters for shell variable names. The
first command format in the following examples sets the variable based
on a name and value selected by the user, while the unset command
removes the variable from the current shell and subshells:
Command Format
VARIABLE=value
unset VARIABLE
Setting a Local Variable
$ DT=/usr/dict
$ echo $DT
/usr/dict
$ cd $DT
$ pwd
/usr/dict
$ unset DT
$ echo $DT
$ cd

$ cd $DT
$ pwd
/home/user2
The echo command simply echoes back to the screen whatever is
passed to it as an argument. The dollar sign ($) metacharacter preceding
a variable name enables the system to use the value of the variable and
not the name of the variable. In the above example, the echo command
with $DT displays the value of DT to the screen. Since this is a local
variable, if a new subshell is opened, the variable DT is not available.
Displaying Shell Variables
Variables and their values can be displayed by typing the set command.
Command Format
set
Displaying Variables
This example displays the shell variables of user1
$ set
BASH=/bin/bash
BASH_ENV=/home/user1/.bashrc
BASH_VERSINFO=([0]=”2” [1]=”05a” [2]=”0” [3]=”1”
4=”release” [5]=”i686-pc-linux-gnu”)
BASH_VERSION=”2.05a.0(1)-release ”
COLORS=/etc/DIR_COLORS
COLUMNS=80
DIRSTACK=()
EUID=500
GROUPS=()
HISTFILE=/home/user1/.bash_history
HISTFILESIZE=1000
HOME=/home/user1
HOSTNAME=user1

HOSTTYPE=i686
IFS=$’ \t\n’
INPUTRC=/etc/inputrc
LANG=en_US.iso885915
LESSOPEN=’|/usr/bin/lesspipe.sh %s’
LINES=25
LOGNAME=user1
LS_COLORS= <output omitted>
MATCHTYPE=i686-pc-linux-gnu
MAIL=/var/spool/mail/user1
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/sbin:
/usr/bin:/home/user1/bin
PIPESTATUS=([0]=”0”)
PPID=1038
PS1=’[\u@\h \W]\$’
PS2=’>’
PS4=’+’
PWD=/home/user1
REMOTEHOST=192.168.20.126
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:histexpand:monitor:histor
y:interactive-comments:emacs
SHLVL=1
SUPPORTED=en_US.iso885915:en_US:en
TERM=vt100
UID=500

USER=vominh
USERNAME=root
_=PATH
langfile=/home/vominh/.i18n
mc ()
{
mkdir -p $HOME/.mc/tmp 2>/dev/null;
chmod 700 $HOME/.mc/tmp;
MC=$HOME/.mc/tmp/mc-$$;
/usr/bin/mc -P "$@" >"$MC";
cd "`cat $MC`";
/bin/rm -f "$MC";
unset MC
}
Note – In your current shell, the display maybe differ from this
example.
Environment Variables
Your computing environment is composed of special information, such as the
location of your mailbox and the type of terminal you have. The Linux system
software provides several default environment variables such as PS1, HOME,
LOGNAME, SHELL, and PATH. These variables have been defined by the shell to
have a specific function.
The values of these customizable variables can be changed to suit the user’s
needs.
You can temporarily change your environment variables at the command line.
This affects only the current shell. When you exit the shell where the
environment variable has been assigned, that environment variable is
terminated or set back to its default value.
Permanent changes are made by modifying the initialization files, discussed in
Module 11, ‘‘Initialization Files.”

Note – Any shell variable can be made available as an environment variable.
Exporting Variables
Exporting variables in an initialization file enables the variables to be
used by the system, processes, scripts, users, and all shells. Exporting
variables at the command line makes the variables available to the
current shell and all of its child processes. A variable set in a subshell
and exported will be available to any shells opened afterwards but will
not be exported to the parent shell. It is good practice to use the echo
command to check if a value for a variable already exists before
setting a new one. This protects accidental overwriting of a previously
set value.
Command Format
VARIABLE=value;export VARIABLE or
export VARIABLE=value
Creating Environment Variables
$ LPDEST=staffp; export LPDEST
$ echo $LPDEST
staffp
$ EXINIT=’set showmode’; export EXINIT
$ echo $EXINIT
set showmode
$ unset LPDEST
$ echo $LPDEST
$

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×