Compunet Corporation
Introduction to Unix (CA263)
Your Environment
By
Tariq Ibn AzizDammam Community College
Compunet Corporation
Objectives
• In this lecture you will learn – Local variable
– Subshells
– Exported Variables
– PS1 and PS2
– Your PATH
Compunet Corporation
Objectives
• In this lecture you will learn – Your Current Directory
– More on Subshells
– Your .profile File
– The TERM Variable
Compunet Corporation
Local Variables[1]
• The vartest consists of echo command that display value of x, surrounded by colons
$ cat vartestecho :$x:$ x=100$vartest::$• vartest doesn’t know about the value of x.
Therefore its value is null.
Compunet Corporation
Local Variables[2]
$ cat vartest2x=50echo :$x:$ x=100$vartest2:50:$• Now question is what is the value of x?$ echo $x100$
Compunet Corporation
Subshells
• The behavior exhibited by vartest and vartest2 is due to the fact that these two program are run as subshells by your login shell.
• When you ask your login shell to execute the vartest, it startup a new shell to execute the program.
• Whenever new shell runs it runs in its own environment with its own set of local variables.
• A subshells has no knowledge of local variables that were assigned values by the login shell.
• A subshell cannot change the value of a variable in the parent shell.
Compunet Corporation
Exported Variables
• There is a way to make the value of a variable known to a subshell, and that’s by exporting it with the export command.
• Here is a program vartest3$ cat vartest3echo x = $xecho y = $y$
$ x=100$ y=10$ vartest3x =y =$ export y$ vartest3x =y = 10$
Compunet Corporation
Exported Variables
• Change the value of x and y in vartest4.$ cat vartest4x=50y=5$vartest4$echo $x $y100 10$
Compunet Corporation
Exported Variables
$ cat vartest4x=50y=5z=1export zVartest5$$cat vartest5echo x = $xecho y = $yecho z = $z$
$ vartest4x=y=10z=1$• When vartest4 get
executed, the exported variable y will be copied into the subshell’s environment.
• vartest4 set the value of x, y and z. The it export z.
• This makes the value of z accessible to any subshell.
Compunet Corporation
Exported Variables Example
$ cat vartest4x=50y=5z=1export y zvartest5$$cat vartest5echo x = $xecho y = $yecho z = $z$
• If you change the vartest4 to explicitly export the value of y, then changed value of y will get exported down to vartest5.
$ vartest5x=y=5z=1$
Compunet Corporation
export with No argument
• If you just type export, not followed by any argument, you will get a list of the exported variables that are exported by your shell.
export LOGNAMEexport PATHexport TIMEOUTexport TZexport y
Compunet Corporation
PS1 and PS2
• The characters that the shell display as your command prompt are stored in the variable PS1. You can change this variable to be anything you like.
• $ echo :$PS1::$ :$ PS1="==>“==>
Compunet Corporation
PS1 and PS2
• Your secondary command prompt, normally > is kept in the variable PS2, where you can change it as you like.
$ echo :$PS2::> :$ PS2="=====>”$
$ for x in 1 2 3=====> do=====> echo $x=====> done123$
Compunet Corporation
HOME variable
• Your home directory is where you are placed whenever you log onto the system.
• A special variable HOME is also automatically set to this directory when you log on.
$ echo $HOME/usr/steve$ HOME=/usr/steve/book$ pwd/usr/steve$ cd$ pwd/usr/steve/book
Compunet Corporation
Your PATH
• Whenever you type in the name of a program to be executed, the shell searches a list of directories until it find the program.
• The list of directories is contained in a special variable called PATH$ echo $PATH/bin:/usr/bin:
• You can always override the path variable by specifying the path to the file to be executed.
Compunet Corporation
Example PATH
• The shell will go directly to /bin to execute date.$ /bin/date
Or./rolo
• You can execute rolo without specifying path if the directory of rolo is specified in PATH variable.
Compunet Corporation
Your Current Directory
$ cat cdtestcd /usr/steve/binpwd$ pwd/usr/steve$ cdtestusr/steve/bin
$ pwd/usr/steve• Why we are in /usr/steve where as we changed the directory in cdtest program?
Compunet Corporation
CDPATH Variable
• The CDPATH variable works like the PATH variable.
• It specifies a list of directories to be searched by the shell whenever you execute the cd command.
• This search is done only if the specified directory is not given by a full path name and if CDPATH is not null.
Compunet Corporation
CDPATH Variable Example
• If you type cd /usr/steve then the shell changes your directory directly to /usr/steve
• But if you type cd memos, then shell will look at your CDPATH variable to find the memo directoy. And if the CDPATH looks like this$ echo $CDPATH:/usr/steve:/usr/steve/docs$ cd /usr/steve$ cd memos/usr/steve/docs/memos$ cd bin/usr/steve/bin
Compunet Corporation
More on Subshells
$ cat varsBOOK=/usr/steve/bookUUPUB=/usr/spool/uucppublicDOCS=/usr/steve/docs/memosDB=/usr2/data$$ vars$ echo $BOOK
$
Compunet Corporation
The . Command
• There is a shell built-in command called . (dot)
. file• Whose purpose is to execute the content of file in
the current shell.
$ . vars$ echo $BOOK/usr/steve/BOOK$
Compunet Corporation
The .profile Command
• Your login shell executes two special files on the system.
• The first is /etc/profile. This file is set up by the two system administrator and usually does things like checking to see if you have mail.
• The second file that get automatically executed is .profile in your home directory. Your system administrator may have given you a default .profile file when you got your account.
Compunet Corporation
The .profile Command
$ cat $HOME/.profilePATH="/bin:/usr/bin:/usr/lbin::"export PATH$
Compunet Corporation
The TERM Variable
• If you tend to use more than one type of terminal, then the .profile is a good place to put some code to prompt for the terminal type and then set the TERM variable accordingly.
• A sample code from .profile file on next slide.
Compunet Corporation
The TERM Variable
echo "what terminal are you using(hp-2621 is default ?\c"
read TERMif [ -z "$TERM" ]then
TERM=2621fiexport TERM