Environment
Unix is a multi-user system. That means multiple users can login and use a single computer running Unix at the same time. When the user logs in the user gets an environment. What is the path value set to for that user ? What is the home directory, the keyboard layout and so on.
Introduction
The shell is a program that sits between the user and the kernel. When we type a command, shell will take that command and execute it for us and display the output to us on the terminal. There are different kinds of shells such as: bash, tcsh, csh, zsh. The default shell for the hill server is "bash". The shells have some differences between them but if we write a command that follows the POSIX standard, the command will run in all of the shells. We can change the shell that we are in by typing the shell executable. The text below shows how to change to csh.
[amittal@hills ~]$ csh
Finding our current shell
[amittal@hills ~]$ ps -p $$
PID TTY TIME CMD
2159711 pts/9 00:00:00 csh
[amittal@hills ~]$
Exiting csh
[amittal@hills ~]$ exit
Going back to the bash shell
[amittal@hills ~]$ ps -p $$
PID TTY TIME CMD
2159633 pts/9 00:00:00 bash
set and printenv
There are 2 kinds of variables used to set and maybe configure the environment. One is the shell and the other is the environment. Shell variables apply to the logged in user shell instance whereas the environment variables are system wide.List all the environment variables with the command: $ printenv | sort > env.txt List both the shell and env variables with the command: $ set > both.txt Open these files in an editor and examine the contents. We see that the below entries exist in "both.txt" HISTFILE=/home/Deller/.bash_history HISTFILESIZE=500 HISTSIZE=500 but do not exist in the file "env.txt". These are shell variables and specific to the shell session. Let us examine a few of the variables. The PATH variable has the following value. /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin: /users/amittal/.local/bin:/users/amittal/bin This contains a sequence of folders. When we type a command or an application name such as "echo" the shell will start looking in each of these folders to find the executable. If it finds it then the shell will run the program. We can use the "which" to find out where the excutable is and we see that it is in the secon folder in the path. [amittal@hills ~]$ which echo /usr/bin/echo A related command is "whereis" . This command locates the binary, source, and manual page files for a command. To find the value of a single variable we can use the "echo" command with a dollar sign. [amittal@hills ~]$ echo $PATH /usr/local/bin:/usr/bin:/usr/local/sbin: /usr/sbin:/sbin:/users/amittal/.local/bin:/users/amittal/bin [amittal@hills ~]$ echo $PS1 [\u@\h \W]\$ This value dictates what our prompt will look like. The "\u" means the username and the "\h" means the host . The "\W" means the the basename of the current working directory, with $HOME abbreviated with a tilde. [amittal@hills ~]$ cd temp [amittal@hills temp]$ Now the "~" changes to the folder we are in "temp" which is relative to the home folder. Let's say we didn't want such a long prompt and are just happy with a single dollar prmopt we can change the prompt with the below commands. The "export" command makes the value availbable to any child processes. [amittal@hills temp]$ export PS1="$" $ $date Sun Jan 7 13:59:33 PST 2024 $ We can see that the prompt changed. However if we close the session and open a new session our changes are not saved. We will see how to make the change permanent in a later section. \u Current user’s username. \h Hostname of the current machine. \# Command number of the current command. Increases with each new command entered. \$ If the effective UID is 0 (that is, if you are logged in as root), end the prompt with the # character; otherwise, use the $. \W Show the current working directory The SHELL variable contains the value of the shell that we are using. $echo $SHELL /bin/bash $
configuration files
To make permanent changes to the environment, we need to edit the configuration files. There are global configuration files and there are also personal configuration files in our home folder. If we go to our home folder and do a "ls -a" we can see our configuration files. They start with the "." symbol[amittal@hills ~]$ ls -a . .history .. .kshrc .bash_history .lesshst .bash_logout .bash_profile .bashrc .cacheEach shell has a different scheme for working with confirugation files. The text below shows how the "bash" does its configuration
bash
/etc/bashrc Global configuration.
/etc/profile Global configuration for login shells.
~/.bash_profile User’s personal configuration file for
login shells.
~/.bashrc User’s personal configuration file for
all subshells.
~/.profile User’s personal configuration file for
all login shells. This file is read if
~/.bash_profile does not exist.
The ".profile" does not exist on our hill server
account because the ".bash_profile" exists.
The system will execute the global configuration files first and then
the configuration files in our system. As users we will not
have access to modify the global configuration files.We can
make changes to the configuration files in our local folder.
As an example we are going to modify ".bash_profile" on our
hills server. It is always a good idea to save the file somewhere.
[amittal@hills ~]$ mkdir save
[amittal@hills ~]$ cp .bash_profile save
#Edit the .bash_profile file
[amittal@hills ~]$ vi .bash_profile
The contents will look like:
[amittal@hills ~]$ cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
export PS1="$"
We placed the statement at the bottom of the file.
export PS1="$"
Now when we open a new session we get the new prompt. Our change does not just apply to the current session but new sessions also.
We can revert our change by bringing back our saved ".bash_profile" .
cp save/.bash_profile . $
Aliases
The "alias" command allows us to give our own name to an exsisting command. Let;s say we wanted to run the "date" coomand but we want to use the word "getdate" .alias getdate="date" $alias getdate="date" $getdate Tue Feb 13 05:31:50 PST 2024 $Lets say we wanted the "ls" coomand to always display the long listing format; that is "ls" with the "-l" option.
alias ls="ls -l" $alias ls="ls -l" $ls total 259540 -r--rwxr-x 1 amittal csdept 30 Feb 10 10:24 1.txt -r--rwxr-x 1 amittal csdept 0 Feb 10 10:27 2.txtThe above shows how we can assign a different meaning to an exsisting command.
This change is only valid for our current session. To make the change permanent we can add it to the local configuration file such as ".bash_profile" .