Linux Bash shell a crash course in Bash shell usage

Kalle Larsson 2014-05-21

Small initial note

If the information is new to you, don’t worry J Don’t get stuck on details if there is something you don’t get right away, you can always go back later and everything can be looked up on Internet.

The prompt

Most of the included example snippets are prefixed  with a $-sign indicating the shell prompt. If testing any of the examples yourself, you are not supposed to type the $-sign.

Quick overview of todays seminar The GUI versus the CLI - A first encounter - Pros and Cons

What is a shell - History - When to use the shell,   and when not to

Interactive use - tab completion - line editing - history

The basics - Variables, Variable   substitution, Functions - Control structures - Scope, Sub-shells, Pipes  

In real life - Gluing, Processing,   Wrapping - A random selection of   building blocks

The CLI vs the GUI – A first encounter The GUI

The CLI

(Graphical User Interface)

(Command Line Interface)

1. To start programs: double   click its icon 2. Startup control: optionally   drag'n'drop something on the   icon, or right-click etc… 3. Runtime control: interactively   click on different menu choices • The way to work with GUI   programs; and it's possible to  

look around a little bit to find that   particular option, whatever it   was called ("xyz...something")

1. To start programs: type its   name and press enter 2. Startup control: add command   line options before pressing enter • Normally the way of controlling   the program; but you need to   know the name of the option you   want to use! (Hint: man pages)

3. Runtime control: optionally   answer or acknowledge   something (Note: NOT always) • Usually only used as a safety  

line, "yes, please go ahead with   removing my file system"

The CLI vs the GUI – Pros and Cons The GUI

Pros • Superior for qualitative control   and overview • Normally easier to "get started"   when new to the program

Cons • Usually some (or a lot of)   mandatory interactive handling   required, makes it unsuitable for   batch processing

The CLI

Pros • Simple to control in an   automated fashion • Work flow can usually be   precisely defined ahead-of-time,   minimizing the need for interactive intervention

Cons • Often a steeper learning curve • Can be difficult to get an    overview of "stuff” • Sometimes “over-powerful” for the users best

What is a shell

What is a shell • It's "the primary computer CLI” • It's a program for interactive or automatic control of which   applications the computer should start. $ matlab

• It's a macro processor. $ rm *.o

• It's a programming language. $ $ > > >

COUNTER=0 while [ $COUNTER -lt 10 ]; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done

• It allows you to easily combine various small commands as   building blocks to create powerful tools.

What is a shell – history • The original Bourne shell (sh) was created by Stephen Bourne   in 1977. • The "Bourne-again shell" (Bash) was created in the late 80s   and is part of the GNU project. Its syntax and set of features   are a superset to the original Bourne shell. • There are other shells as well. Some Bourne shell compatible, such as Korn shell and Busybox*, and some not. The csh-family are possibly the second most common type of shell. • This presentation focuses on Bash

*Busybox is single executable file for embedded systems that includes Bourne-shell and a subset of common Linux utils and tools

What is a shell – Bash Warning about Bash syntax -  Unfortunately, the syntax, and especially the multitude of usages   of special characters in different contexts, can be daunting: $( ), $(( % * / + - A | & # )), ( ), (( )), !, !!, !n, [ ], [[ = ~= ]], { },${ }, ;, ;;, &, &&, #, ${ :- := :? :+ : :: !* !@ # ## % %% // },