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?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

The Linux Command Line

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

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Ad