Bash:Bash-Shell-Expansion

From Juneday education
Jump to: navigation, search

Work in progress.

Introduction

This short chapter introduces bash shell expansion. This is a very powerful way of expressing strings in bash. Using certain operators and symbols, we can make bash expand an expression before using it, for instance as an argument to a command. All expansion occurs before the resulting strings are used.

Brace expansion

Brace expansion uses curly braces, { and } to produce combinations of strings. We'll explain by example in this small introduction.

Let's say we want to produce the following strings:

  • SVT1
  • SVT2
  • SVT24

We can do this by observing that they follow a pattern. They start with "SVT" and then one of "1", "2", and "24".

This is how you can produce those combinations, and echo the result to the terminal:

$ echo SVT{1,2,24}
SVT1 SVT2 SVT24
$

Brace expansion works by taking the surrounding string literal(s) and making combinations using a comma separated list within braces.

We can even use intervals:

$ echo SVT{1..24}
SVT1 SVT2 SVT3 SVT4 SVT5 SVT6 SVT7 SVT8 SVT9 SVT10 SVT11 SVT12 SVT13 SVT14 SVT15 SVT16 SVT17 SVT18 SVT19 SVT20 SVT21 SVT22 SVT23 SVT24
$

We can nest brace expressions inside one and other. Let's say we want to produce: SVT1 SVT2 SR1 SR2 SR3. The pattern here is a little more complicated. We want all combinations of SVT followed by 1, 2 and SR followed by 1,2,3:

$ echo {SVT{1,2},SR{1..3}}
SVT1 SVT2 SR1 SR2 SR3
$

Or, simply:

$ echo SVT{1,2} SR{1..3}
SVT1 SVT2 SR1 SR2 SR3

Nesting brace expressions is really powerful when creating directory trees. Let's pretend we want to create the following tree:

music/
├── classical
│   ├── classicism
│   ├── modernism
│   ├── modernist
│   └── renaissance
├── jazz
│   ├── bebop
│   ├── free_jazz
│   └── fusion
└── rock
    ├── hard_rock
    ├── metal
    └── rockabilly

This can be achieved with one single command line:

$ mkdir -p music/{classical/{modernist,renaissance,classicism,modernism},rock/{hard_rock,metal,rockabilly},jazz/{bebop,free_jazz,fusion}}

Why does it work?

Let's have echo printing the expanded string out:

$ echo music/{classical/{modernist,renaissance,classicism,modernism},rock/{hard_rock,metal,rockabilly},jazz/{bebop,free_jazz,fusion}}
music/classical/modernist music/classical/renaissance music/classical/classicism music/classical/modernism music/rock/hard_rock music/rock/metal music/rock/rockabilly music/jazz/bebop music/jazz/free_jazz music/jazz/fusion

Intervals in brace expressions can have a step part. Let's say we want to print out every fourth number between 1 and 20 inclusive:

$ echo {1..20..4}
1 5 9 13 17

Starting at 0, we get:

$ echo {0..20..4}
0 4 8 12 16 20

And it works for letters too!

$ echo {a..m}
a b c d e f g h i j k l m

And, even with steps!

$ echo {a..m..3}
a d g j m

Also, intervals can go down:

$ echo {20..0}
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

$ echo {20..0..4}
20 16 12 8 4 0

Expansion of tilde ~

TODO

Expansion of variables

TODO

Environment variables

TODO

Arguments to scrips

TODO

Creating your own variables

TODO

Some special variables

TODO

Links

Further reading

Where to go next

Next page is about the standard streams.

« PreviousBook TOCNext »