The shell environment

INPUT · Slides

Aliases and history

01 / 08

How many times have you typed the same thing?

By now you have typed commands like these over and over.

ls -ldu -sk * | sort -n | tail -1grep -n error log.txt

Long, and easy to mistype.

The shell has two arrangements to make it easier.

aliases   give a long command a short namehistory   keep the moves you typed, and call them back

Both are tools for fewer moves. Today you do the two of them.

These two are, in fact, where the good people differ most. Two people who can do the same thing can differ threefold in the moves they type.

someone new    40 characters for one thingsomeone used to it   8 characters for one thing

The reduction comes from knowing the tools. Let us look.

02 / 08

Give a short name with alias

Type this and you have a short name.

~ $ alias ll="ls -l"~ $ ll-rw-r--r--  1 learner ...

Two characters, ll, and ls -l ran. That is an alias (another name).

The shape is this.

alias short-name="the real command"

Like assigning a variable, is it not. No spaces around the =, the same as there.

Type alias alone and you get a listing of what you have made.

~ $ aliasll='ls -l'

To remove one there is unalias.

~ $ unalias ll

And there is one rule that catches people out.

~ $ alias f="echo"; f hi-sh: f: not found      <- not usable on the same line

The shell reads the whole line before running it, and at reading time it did not yet know f. Once you have made one, use it from the next line.

03 / 08

You can add to the end

An alias is plain replacement, so you can add what you like at the end.

~ $ alias g="grep -n"~ $ g error log.txt

When you type it, the shell puts this together.

g error log.txt  | replace ggrep -n error log.txt

It only replaces the front of the line, so the rest goes over as it is.

You can keep a whole pipe in one.

~ $ alias big="du -sk * | sort -n | tail -3"~ $ big

The "find the culprit" run of chapter 9, in three characters.

The replacing happens at the front of the line only, mind.

~ $ alias t="tail -3"~ $ ls | t          <- there are places where this will not work

Straight after a pipe it works, but in the place of an argument it does not. Think of it as a name that comes first and you are safe.

04 / 08

' and " change the meaning

Here the quoting of the last lessons earns its keep. These two differ.

~ $ alias a="echo $USER"~ $ alias b='echo $USER'~ $ alias aa='echo learner'      <- settled already~ $ alias bb='echo $USER'         <- still $USER

Doubles expand when you make it; singles expand when you use it.

"..."   fixed at the value of the moment you defined it'...'   the value of the moment, each time you call it

Which is better depends on what you want.

alias home='cd $HOME'        the $HOME of when you use it -> singlesalias me="echo $USER"        fix it at the current name -> doubles

When in doubt, singles is the usual choice. An alias is "a sentence to be used later", so expanding later is the natural thing.

The table from the last lesson has turned straight into a tool.

singles  expand nothingdoubles  expand the $ ones only

05 / 08

Use \ when you want the real one

An alias can have the same name as a real command.

~ $ alias ls="ls -1"~ $ ls(out it comes in one column)

Handy, and now and then you want the real one. Then you put a \ in front.

~ $ \ls(the real ls runs)

Putting command in front does the same.

~ $ command ls

Both mean "skip the alias and call the real one".

This really does help in the field.

alias rm="rm -i"     always ask before removing (for safety)\rm many-files-*     skip it only when removing a lot

Safe by default, off only when you need it.

Remember that type tells you what something is, too.

~ $ type llll is an alias for ls -l

You learned type in the second lesson of this chapter. To the "builtin or outside tool" of that day, an alias has been added.

06 / 08

An alias does not pass to a child

Try the experiment you did with variables, on an alias.

~ $ alias ll="ls -l"~ $ sh -c "alias"(nothing comes out)

It did not pass. An alias cannot be exported.

an environment variable   export it and it passes to childrenan alias                  it does not (it belongs to that shell)

So take it that aliases are unusable inside a script, a script you write running in a child shell.

#!/bin/shll              <- will not workls -l           <- write it like this

In a script you write it out properly, without shortening. That is the difference between typing by hand and writing something down.

typing by hand    you want it short     -> an aliaswriting it down   you want it to work   -> no shortening

The distinction helps when you write scripts in chapter 11.

07 / 08

The moves you typed are still there

The other tool: history.

Press and the command before this one comes back. There is a in the symbol bar at the bottom of the screen. Press it.

↑        one back↑↑       two back

What comes back can be typed again with as it is. When you mistype something, calling it back with and fixing it is the fastest thing.

You can see them as a list too.

~ $ history    1 pwd    2 ls -l    3 alias ll="ls -l"

One thing to watch. The output of history comes out on standard error.

history > a.txt      you get nothing (it comes out empty)history 2> a.txt     you get it

The 1> and 2> of chapter 4. Knowing there are two ways out lets you handle an oddity like this.

The history is kept in a file too.

~ $ echo $HISTFILE/home/learner/.ash_history

The name starts with ., so you need ls -a to see it. The hidden files of chapter 1.

08 / 08

Now have a go

Here are the shapes for this lesson.

alias ll="ls -l"       give a short namealias                  see the listingalias ll               see just oneunalias ll             remove oneunalias -a             remove them alltype ll                find out what it is\ls                    call the real onehistory 2> a.txt       take the history↑                      call back the last command

One thing to say in advance.

> An alias goes when you close the terminal

What you make belongs to that shell alone. Open it next time and there is nothing.

made now         ->  this terminal onlywanted always    ->  the next lesson

The next lesson is the last of chapter 10, .profile. There is a place that gets read every time you open one, and writing them there makes them yours for good.

Which makes today also the lesson where you decide what you want every time. As you type, remember the ones you think "I want this always". Now let us type.