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
/usr/local/git/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/JohnDoe/.composer/vendor/bin:/Users/JohnDoe/.composer

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))
10

Here are some more arithmetic notations you can use:

+
Addition.
-
Subtraction.
*
Multiplication.
/
Division (integer division, so will floor any decimals).
**
Exponentiation.
%
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.

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

Linux for Beginners

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

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Ad