October 31, 2019

Good Morning

Saying “Good Morning”

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?

What time is it?

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

A simple clock

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?

Doing the same several times

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

Printing the value of 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

UNIX Variables

There are two type of UNIX variables:

  • local variables: apply only to the current instance of the shell and are used to set short-term working conditions
    • The i variable is a local variable
  • Environment variables are a way of passing information from the shell to programs when you run them.

Programs look “in the environment” for particular variables and if they are found will use the values stored.

Environment Variables

An example of an environment variable is OSTYPE

The value of this is the current operating system you are using

echo $OSTYPE
darwin19

Environment Variables

More examples of environment variables are

  • USER: your login name
  • HOME: the path name of your home directory
  • LANG: the language of the error messages
  • PATH: the directories the shell should search to find a command
  • PS1: the shell’s prompt

Finding out the current values of these variables.

Local 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

Finding out the current values of these variables

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

Using and setting variables

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.

Setting shell variables in the .bashrc file

For 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

Making permanent changes

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

Modifying the configuration

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

Re-processing .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

Setting the path

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

  • the command doesn’t exist at all on the system
  • or it is not in your path

Example

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

Adding folders to 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

Other things to do in your .bashrc

Some command options are useful

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

Aliases

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

Aliases are valid for one session

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