09. Expansions echo

Just before the shell carries out your command, it expands it out.

For example, when using a ? wildcard, the shell expands it out so that it can check for matches on every possible letter.

The echo command

The echo command is used to display a line of text.

For example, we can print out a single line of text.

$ echo 'Hello world!'
Hello world!

Why is this at all useful? You'll see that we manipulate this command to have the shell evaluate an expression, and return the value to us.

Variable Expansion

If we'd like to see the contents of a variable, we can specify it as an argument to echo.

$ echo $PATH

Pathname Expansion

Firstly, we can use echo to see the contents within our current path.

$ echo *

This would show all the contents within our current working directory.

If we used a ~ (tilde) is expanded into a home directory.

Arithmetic Expansions

Arithmetic expressions can be evaluated using the dollar symbol with two parantheses. For example,

$ echo $((5 * 2))

Here are some more arithmetic notations you can use:

Division (integer division, so will floor any decimals).
Modulo (remainder).

Brace Expansion

We can also create custom expansions, and check to see how the shell interprets it.

If we wanted three files called front-X-back, front-Y-back and front-Z-back, we can just type:

$ echo front-{X,Y,Z}-back
front-X-back front-Y-back front-Z-back

This is very powerful, especially if you want to create a directory with consecutive pattern! Let's say, for example, you want to create multiple folders for your photo, per month since you were born.

$ mkdir {1984..2015}-0{1..9} {1984..2014}-{10..12}
# Don't actually run this command unless you want to make a lot of folders!

Specifying a range

Furthermore, if you'd like to give a range, simply specify two periods between the min and max.

$ echo myFolder{A..F}
myFolderA myFolderB myFolderC myFolderD myFolderE myFolderF
$ echo myFolder{1..6}
myFolder1 myFolder2 myFolder3 myFolder4 myFolder5 myFolder6 

Nested expansion

You can even nest expansions!

$ echo {1{a..f}9{A..F}}
{1a9A} {1a9B} {1a9C} {1a9D} {1a9E} {1a9F} {1b9A} {1b9B} {1b9C} {1b9D}
{1b9E} {1b9F} {1c9A} {1c9B} {1c9C} {1c9D} {1c9E} {1c9F} {1d9A} {1d9B}
{1d9C} {1d9D} {1d9E} {1d9F} {1e9A} {1e9B} {1e9C} {1e9D} {1e9E} {1e9F} 
{1f9A} {1f9B} {1f9C} {1f9D} {1f9E} {1f9F}

As you can imagine, this is much faster than right click - new folder - type in name, repeat.

Take your Linux skills to the next level!

How Linux Works

Take your Linux skills to the next level! Try Linux & UNIX

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources