Skip to content
2 changes: 2 additions & 0 deletions docs/books/learning_bash/.pages
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
title: Learning Bash
59 changes: 59 additions & 0 deletions docs/books/learning_bash/00-toc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
title: Learning bash with Rocky
author: Antoine Le Morvan
contributors: Steven Spencer
tested with: 8.5
tags:
- education
- bash scripting
- bash
---

# Learning Bash with Rocky

In this section, you will learn more about Bash scripting, an exercise that every administrator will have to perform one day or another.

## Generalities

The shell is the command interpreter of Linux.
It is a binary that is not part of the kernel, but forms an additional layer, hence its name "shell".

It parses the commands entered by the user and then executes them by the system.

There are several shells, all of which share some common features.
The user is free to use the one that suits him/her best. Some examples are:

* the **Bourne-Again shell** (`bash`),
* the **Korn shell** (`ksh`),
* the **C shell** (`csh`),
* etc.

`bash` is present by default in most (all) Linux distributions.
It is characterized by its practical and user-friendly features.

The shell is also a **basic programming language** which, thanks to some dedicated commands, allows:

* the use of **variables**,
* **conditional execution** of commands,
* the **repetition** of commands.

Shell scripts have the advantage that they can be created **quickly** and **reliably**, without **compiling** or installing additional commands. A shell script is just a text file without any embellishments (bold, italics, etc.).

!!! NOTE

Although the shell is a "basic" programming language, it is still very powerful and sometimes faster than badly compiled code.

To write a shell script, you just have to put all the necessary commands in a single text file.
By making this file executable the shell reads it sequentially, and executes the commands in it one by one.
It is also possible to execute it by passing the name of the script as an argument to the bash binary.

When the shell encounters an error, it displays a message to identify the problem but continues to execute the script.
But there are mechanisms to stop the execution of a script when an error occurs.
Command-specific errors are also displayed on the screen or inside files.

What is a good script? It is:

* **reliable**: its operation is flawless even in case of misuse;
* **commented**: its code is annotated to facilitate the rereading and future evolution;
* **readable**: the code is indented appropriately, the commands are spaced out, ...
* **portable**: the code runs on any Linux system, dependency management, rights management, etc.
123 changes: 123 additions & 0 deletions docs/books/learning_bash/01-first-script.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
---
title: Bash - First script
author: Antoine Le Morvan
contributors: Steven Spencer
tested with: 8.5
tags:
- education
- bash scripting
- bash
---

# Bash - First script

In this chapter you will learn how to write your first script in bash.

****

**Objectives**: In this chapter you will learn how to:

:heavy_check_mark: Write your first script in bash;
:heavy_check_mark: Execute your first script;
:heavy_check_mark: Specify which shell to use with the so-called shebang;

:checkered_flag: **linux**, **script**, **bash**

**Knowledge**: :star:
**Complexity**: :star:

**Reading time**: 10 minutes

****

## My first script

To start writing a shell script, it is convenient to use a text editor that supports syntax highlighting.

`vim`, for example, is a good tool for this.

The name of the script should respect some rules:

* no names of existing commands;
* only alphanumeric characters, i.e. no accented characters or spaces;
* extension .sh to indicate that it is a shell script.

!!! note

The author uses the "$" throughout these lessons to indicate the user's command-prompt.

```
#!/usr/bin/env bash
#
# Author : Rocky Documentation Team
# Date: March 2022
# Version 1.0.0: Displays the text "Hello world!"
#

# Displays a text on the screen :
echo "Hello world!"
```

To be able to run this script, as an argument to bash:

```
$ bash hello-world.sh
Hello world !
```

Or, more simply, after having given it the right to execute:

```
$ chmod u+x ./hello-world.sh
$ ./hello-world.sh
Hello world !
```

!!! note

To execute the script, it needs to be called with `./` before its name when you are in the directory where the script resides. If not in that directory, you will need to call it with the entire path to the script, OR place it in a directory that is within your PATH environment variable: (Examples: `/usr/local/sbin`, `/usr/local/bin`, etc.)
The interpreter will refuse to execute a script present in the current directory without indicating a path (here with `./` before it).

The `chmod` command is to be passed only once on a newly created script.

The first line to be written in any script is to indicate the name of the shell binary to be used to execute it.
If you want to use the `ksh` shell or the interpreted language `python`, you would replace the line:

```
#!/usr/bin/env bash
```

with :

```
#!/usr/bin/env ksh
```

or with :

```
#!/usr/bin/env python
```

This first line is called the `shebang`.
It starts with the characters `#!` followed by the path to the binary of the command interpreter to use.

!!! hint "About the shebang"

You may have encountered the "shebang" in a script that you've looked at that does not contain the "env" section and simply contains the interpreter to use. (Example: `#!/bin/bash`). The author's method is considered to be the recommended and proper way to format the "shebang".

Why is the author's method recommended? Because it increases the portability of the script. If for some reason the interpreter lived in an entirely different directory, the interpreter would **still** be found if you used the author's method.

Throughout the writing process, you should think about proofreading the script, using comments in particular:

* a general presentation, at the beginning, to indicate the purpose of the script, its author, its version, its use, etc.
* during the text to help understand the actions.

Comments can be placed on a separate line or at the end of a line containing a command.

Example:

```
# This program displays the date
date # This line is the line that displays the date!
```
Loading