The shell environment

INPUT · Slides

Writing it down in `.profile`

01 / 08

What you made is gone

Call the alias you made last lesson once more.

~ $ ll-sh: ll: not found

Not there. It goes when you close the terminal. The same for variables.

VAR=value      goneexport VAR     gonealias l=...    goneadded to PATH  gone

Everything you did in chapter 10, gone. Making it all again every time is asking too much.

So there is an arrangement for it.

> When you open a terminal, a certain file gets read for you

Write in that file and the settings are in force the moment you open one. Today you learn where that place is.

With this you get a shell of your own. The same Linux feels different from person to person, and this is why.

02 / 08

Write in ~/.profile

The file that gets read is this one.

~/.profile

The name starts with .. It is a hidden file, from chapter 1, so you need ls -a to see it.

~ $ ls -a.  ..  .profile

The contents are an ordinary run of commands. There is no special way of writing.

export EDITOR=/bin/viPATH="$PATH:$HOME/bin"alias l='ls -1'alias h5='head -5'

You just write what you would type. What you typed in the last lessons, lined up as it was.

The usual tools do for making it, too.

~ $ echo 'alias l="ls -1"' > ~/.profile~ $ echo 'export EDITOR=/bin/vi' >> ~/.profile

> for the first line, >> for what follows. The distinction from chapter 4. Add with > by mistake and what you wrote before is gone, so mind that.

You can write it with vi too, of course. That comes in chapter 13.

03 / 08

Read it right now with .

Once written you want to try it. But closing and reopening the terminal is a bother.

There is a command to read it right now.

~ $ . ~/.profile

The . at the front (a single dot) is the command. Hard to read, but it means "read this file in the shell I am in".

~ $ . ~/.profile~ $ l(the alias you just wrote runs!)

Writing source does the same.

~ $ source ~/.profile

The meaning is the same and source reads better. But . is in every shell, so remembering . is the surer thing.

There is an important difference here. It is not the same as running it with sh.

sh ~/.profile      runs in a child shell -> nothing stays in this shell. ~/.profile       runs in this shell -> it stays

"It passes to a child, but does not come back to the parent", as you did in the earlier lessons. When you read settings, running it in this shell is the only thing that means anything. Which is why you use ..

04 / 08

/etc/profile is for everyone

In fact something gets read before your .profile. Shall we look?

~ $ cat /etc/profileexport PATH="/bin:/sbin:/usr/bin:/usr/sbin"...export EDITOR='/bin/vi'

So PATH was settled here! What you saw when you looked at echo $PATH in the second lesson of chapter 10 came from this file.

The difference between the two places is this.

PlaceFor whomWho may write
/etc/profileeveryone on this machineroot only
~/.profileyou aloneyou

And they are read in that order.

1. /etc/profile     everyone2. ~/.profile       you

Whatever is read later wins, so you can override it in your own .profile.

/etc/profile   EDITOR=/bin/vi~/.profile     export EDITOR=/bin/nano   <- this one takes effect

The "root and me" divide of chapter 7 exists in settings files too. Leave the shared one alone and add your own is the manner of it.

05 / 08

What is worth writing

Roughly these four things.

1. adding to PATH2. aliases3. naming the tools to use (EDITOR and so on)4. how it looks (PS1)

The first goes like this.

PATH="$PATH:$HOME/bin"

Putting $PATH: at the front is the important part. Forget it and the places you had go, and even ls stops working. The accident of the second lesson of chapter 10.

You can write it a bit more carefully.

[ -d "$HOME/bin" ] && PATH="$PATH:$HOME/bin"

[ -d ... ] is the shape for checking "is the container there", and && was "if the one before went well, do the next". So it means add it only when it is there.

You can write comments, too.

# my settingsalias l='ls -1'

From # to the end of the line is ignored. Writing the reasons for yourself in six months is a help.

You can change how it looks as well.

export PS1='$ '

The $ you see now is this. It was written in /etc/profile too, was it not.

06 / 08

A mistake is not the end

There is a worry, is there not. If I write it wrong, will I be locked out?

Let us try.

~ $ echo nosuchcmd > ~/.profile~ $ . ~/.profile-sh: /home/learner/.profile: line 1: nosuchcmd: not found

An error, yes. But the shell keeps going.

skip only the line with the error, and carry on from the next

That is how it is built. So one bad line and the other settings still take effect.

The most dangerous thing is this.

PATH="$HOME/bin"        <- $PATH: forgotten

Do that and neither ls nor cat works. But you can fix it.

~ $ PATH=/bin:/sbin:/usr/bin:/usr/sbin~ $ /bin/vi ~/.profile

Put PATH back by hand and the tools work again. Breaking it and fixing it in the second lesson of chapter 10 was for this.

Remember one more way out.

~ $ mv ~/.profile ~/.profile.off

Change the name and it stops being read, so you are back to a blank state. Setting it aside rather than removing it is the skilful way.

07 / 08

Why you see so many names

Look things up and all sorts of names come out.

.profile        common to the sh family.bash_profile   bash, for logging in.bashrc         bash, for interactive use.zshrc          for zsh

Too many, and confusing. Tidied up, it goes like this.

which shell  x  what kind of start

The names are split by that multiplication.

for logging in    read once, when you come infor interaction   read every time you open a terminal

In this environment, ~/.profile is all you need to remember. There is one shell here, and $ENV is not in use.

When a real machine puzzles you, look it up like this.

~ $ echo $0        which shell am I in~ $ ls -a ~        which settings files are there

Check the ground under your feet first. More use than memorising names.

And the thinking is the same in every shell.

type and try  ->  if it was good, write it in the settings file  ->  automatic from then

Know that flow and the file name is only a thing to look up.

08 / 08

Now have a go

Here are the shapes for this lesson.

ls -a ~                see the hidden filescat ~/.profile         see the contentsecho '...' > ~/.profile   write the first lineecho '...' >> ~/.profile  add what follows. ~/.profile           read it right nowcat /etc/profile       read the settings for everyone

This is the last lesson of chapter 10. Look back over it.

LessonWhat you learned
1variables and export
2PATH and how commands are found
3choosing between the quotes
4the order things expand in
5aliases and history
6writing it down in .profile

One to five were typing and trying; six is writing it down. That shape comes round many times from here.

The next chapter is scripts, at last.

.profile     a procedure read when you open a terminala script     a procedure you call yourself

Both are the same thing: writing a procedure in a file. Which means that today you have written your first program. Now let us type.