1.1 About Bash

Bash is short for Bourne-again shell. It is a very popular interactive command line interpreter, or shell.

Bash is the default shell on Mac OS 10 and Linux, making it incredibly widespread.

Bash brings to the table some nice built-ins, which make building scripts a lot easier. Echo, printf, read, declare and some of the ones I won't cover here but are very handy, like bind and alias.

In addition to these built-ins, Bash provides some very useful features like expansions, shell arithmetic, and the extended test construct.

1.2 Bash Commands

Here's a quick reveiw of some Bash commands to get started.

pwd

Prints the working directory. If you run a command, this is where the system will execute it. If you say, delete all the files here, it'll delete all the files in this current working directory.

On Macs, your home folder will be inside the /users folder at the root of the drive.

ls

Lists the files and folders in the current working directory.

List files in a specific folder

ls ~/Desktop/my-files

Show the difference between files and folders

ls -l

Returns a list where there is d at the beginning of each line for a directory

drwxr-xr-x  24 smerth  staff   816B  8 Feb 13:46 Writing-Books/
-rw-r--r--@  1 smerth  staff   2.3K 10 Feb 09:05 chapter-6.md

Lookup options for a command in the man page (short for manual pages)

man ls

Press the spacebar to page through. Press h to bring up the reference for navigating around. And when you're done, just press q.

To make directory:

mkdir people

I'll make one called people.

Delete a directory:

rmdir

Clear the screen

clear

To change directory:

cd

Make a copy of the file:

cp

followed first by the file I want to copy, and then the name of the file I want to copy it to

cp maple.txt new_maple.txt

Remove the file:

rm

Concatenate or stick files together:

cat file.one file.two

But it is most often used to read files. Press spacebar to move through the pages. And q when you're done.

Peek at the beginning of a file with

head some.file

Look at the end of a file:

tail

This is really handy for log files.

1.3 Tilde and brace expansion

The tilde character to represents your home directory. Thats an example of expansion. The tilde character represents the value of the user's home variable.

~

The tilde followed by a dash or a minus represents the batch variable called old PWD, which is the directory that you were just in, if you've recently changed directories.

~-

Another handy type of expansion is called brace expansion. This is written with braces around an expression, and it can help with repeated commands with different terms or interpolation within a range.

This is expansion:

touch {apple,banana,orange}

The touch command executes on each item in the braces, like performing a loop on an array.

N.B. No spaces between the items in the list!!

This is interpolation

touch file_{1..1000}.txt

Makes and names 1000 files!

MacOS seems to have an upper limit of about 14,000 iterations...


To pad the expansion with zeros..

touch file_{01..1000}.txt

This only works in Bash 4


Try

echo {1..10..2}

counts from 1 to 10 in intervals of 2

This only works in Bash 4


works with text

echo {a..z}

and

echo {A..z}

returns all the upper-case letters followed by all the lower-case letters.

This only works in Bash 4


Expansion will try to interpret what you give it.

echo {w..d..2}

returns letters from w back down to d at an interval of 2

This only works in Bash 4


These expressions can be chained together

touch {banana,apple,orange}_{1..100}-{w..d}

Results in 1000's of files named as you might expect.


Get a quick count of the files by piping the list of files into word count with the flag for count by line:

ls -1 | wc -l