October 31, 2019
To print the message “Good Morning”, we use the command
echo Good Morning
echo
just repeats its arguments. For example
echo Repeat after me
Repeat after me
What is the difference with cat
?
To print the current time, we use the command
date
Thu 24 Oct 2019 14:41:17 +03
The output shows when this slides were compiled
We could make a simple clock with these commands
date sleep 5 date sleep 5 date sleep 5 date sleep 5
Can we make it automatically?
To repeat some commands several times, we use
for i in 1 2 3 4 5 6 7 8 9 do date sleep 5 done
The letter i
represents a variable
for i in 1 2 3 4 5 6 7 8 9 do echo The value of i is $i done
The combination $i
means the current value of the variable i
There are two type of UNIX variables:
i
variable is a local variablePrograms look “in the environment” for particular variables and if they are found will use the values stored.
An example of an environment variable is OSTYPE
The value of this is the current operating system you are using
echo $OSTYPE
darwin19
More examples of environment variables are
USER
: your login nameHOME
: the path name of your home directoryLANG
: the language of the error messagesPATH
: the directories the shell should search to find a commandPS1
: the shell’s promptLocal variables are set using the =
symbol
var="value" echo $var
value
They can be unset by using the unset
command.
To show all values of local variables, type
set | less
environment variables are set using the export
command
VAR="value" export VAR echo $VAR
or, in short
export VAR="value" echo $VAR
To show all values of these variables, type
printenv | less
Each time you login to a UNIX host, the system looks in your home directory for initialization files
Information in these files is used to set up your working environment
The shell uses two files called .profile
and .bashrc
(note that both file names begin with a dot).
Both files are processed by the shell when you log in
.profile
versus .bashrc
.profile
is to set conditions which will apply to the whole session and to perform actions that are relevant only at login
.bashrc
is used to set conditions and perform actions specific to the shell and to each invocation of it.
The idea is to set environment variables in the .profile
file and local variables in the .bashrc
file.
.bashrc
fileFor example, to change the number of shell commands saved in the history list, you need to set the shell variable history
It is set to 500 by default, but you can increase it
HISTSIZE=2000
Check this has worked by typing
echo $HISTSIZE
However, this has only set the variable for the lifetime of the current shell
If you open a new connection, it will only have the default HISTSIZE
value set
To permanently set the value of history, you will need to add the set command to the .bashrc
file
Open the .bashrc
file in a text editor
An easy, user-friendly editor is nano
nano ~/.bashrc
Add the following line after the other commands
HISTSIZE=200
.bashrc
Save the file and force the shell to reread its .bashrc
file buy using the source
command.
source .bashrc
Check this has worked by typing
echo $HISTSIZE
When you type a command, your path (or PATH) variable defines in which directories the shell will look to find the command you typed
If the system returns a message saying “command: Command not found”, then either
I have a program in my /home/andres/bin
folder
The command is called clock
To run clock
, you either need to directly specify the clock
path (/home/andres/bin/clock
)
or you need to have the directory /home/andres/bin
in your path
You can add it to the end of your existing path with the command:
PATH=$PATH:/home/andres/bin
Test that this worked by trying to run clock
without the full path
clock
To add this path permanently, add the following line to your .bashrc
after the other commands
export PATH=$PATH:/home/andres/bin
.bashrc
Compare these
who | grep 'an'
who | grep --color=auto 'an'
The second one is easier to understand
But writing grep --color=auto
each time is hard
You can define short names to long commands
alias grep='grep --color=auto'
Now every time you write grep
, the shell does grep --color=auto
If you create an alias, and then you close the session, the alias is lost
You want to create aliases automatically at the start of each session
For that you write them in the .bashrc
file