The GNU/Linux Shell

Hello guys,

I’ve decided to refresh my knowledge about the GNU/Linux Shell because such as many of you I use it nearly everyday but I am not a professional and it has many commands I do not master or even know about.

I wonder what was the best method to learn more about the Shell ? I didn’t find out an answer to this question so I started to watch tutorials on YouTube.

I chose to follow the tutorial on this YouTube channel: The Linux Channel by Kris Occhipinti

Important: the Shell used for all commands is BASH (Bourne- Again SHell).

Here are my notes about what I thought is important to remember about each video.

Episode 1 – What is the Linux Shell tutorial

Link to the video

The Shell is an interpreter for your commands or your script files. It has different shells available on GNU/Linux.

It has built-in commands and external commands. The built-in commands are part of each existing shell. The external commands are kind of additional commands such as grep, sed and awk which are not include by default in your shell.

Check the shell you use

The command is:

1
echo $0

or

1
echo $SHELL

The output is:

/bin/bash

or

/bin/zsh

or another shell.

Check the available shells on your system

The command is:

1
cat /etc/shells

The output is:

/bin/sh
/bin/dash
/bin/bash
/bin/rbash
/bin/zsh

Episode 2 – BASH vs ZSH Linux Shell Tutorial

Link to the video

The episode shows the more advanced auto-completion in zsh in comparison with bash.
I have nothing else to say about this episode.

Episode 3 – Writing your First Shell Script for Linux Tutorial

Link to the video

Write text to the screen

The command is:

1
echo "Hello World!"

Another method to write something on the screen is to write a text into a variable and then to write the content of the variable on the screen.
The command is:

1
2
3
read x
Hello World!
echo $x

The output is:

Hello World!

Ask a question to the user

You can write a question to ask to enter a number.
The command is:

1
read -p "Enter a number: " x

The output is:

Enter à number:

Then you can check the value of the x variable.
The command is:

1
echo $x

Read a file content

The command is:

1
cat myfile.txt

Here is an example.
The command is:

1
cat fruits.txt

The output is:

Apple
Banana
Orange
Pineapple

Append text to a file

Add a text to an existing file.
The command is:

1
echo "Pear" >> fruits.txt

The output is:

Apple
Banana
Orange
Pineapple
Pear

Overwrite text into an existing file

Overwrite the content of a text file.
The command is:

1
echo "Kiwi" > fruits.txt

Now, you only have the word “Kiwi” in your file fruits.txt.
The output is:

Kiwi

Episode 4 – User Input Linux Shell Script Tutorial

Link to the video

Just a video to review what Kris teaches in previous videos.

Episode 5 – Variables in the Linux Shell Script Tutorial

Link to the video

Set a variable.
The command is:

1
x = "Hello World!"

Check the value of x.
The command is:

1
echo $x

The output is:

Hello World!

Another example to set a variable and to use it.
The command is:

1
name = "Didier"

Check the value of name.
The command is:

1
echo $name

The output is:

Didier

Sometimes it can have some problem. I’ll give you an example.
The command is:

1
echo "Hello $name, you own me $5."

The output is:

Hello Didier, you own me .

You can see that the text “$5” is not shown. It’s because the shell consider it as a variable and not as a standard text.
To avoid this little problem, you have to add a “\” in front of the $ to indicate to the shell it’s a dollar sign and not a variable.
The command is:

1
echo "Hello $name, you own me \$5."

The output is:

Hello Didier, you own me $5.

Yeah we did it !

Episode 6 – Linux Shell Script Tutorial the Echo Command

Link to the video

Difference between simple and double quotation marks and the use of double exclamation marks (!!)

It has a difference between simple (‘) and double (“) quotation marks in a bash shell. Here is an example.

The command is:

1
echo Hello World!!

The output is:

Hello Worldecho Hello World!

It show the text and !! equal to your last command executed. So echo “Hello World!” in my example.
To avoid this problem with double exclamation mark (!) you have 2 possibilities.
The first one is to add “\” in front of the exclamation marks. No simple or double quotation marks.
The command is:

1
echo Hello World\!\!

The output is:

Hello World!!

The “\” indicates to the shell to ignore the character as special but consider it as a normal character.

The second possibility is to use simple quotation marks.
The command is:

1
echo 'Hello World!!'

The output is:

Hello World!!

Info: you cannot escape an exclamation mark within double quotation marks. If you wanna “echo” a text with double exclamation marks between double quotation marks you have to disable history expansion in bash before with “set +H”.
The command is:

1
2
set +H
echo "Hello World!!"

The output is:

Hello World!!

Episode 7 – History in the Linux Shell Script Tutorial

Link to the video

The history system allows you to see and use the commands you’ve already typed in the past. You can navigate between your old commands with the arrow keys up and down of your keyboard.

Search a command in history

The command is:

1
Ctrl + r

The output is:

reverse-i-search:

You can type a part of a command and the search system will show the commands containing your term.

Here is an example.
The command is:

1
2
Ctrl + r
ls

The output is (for example):

reverse-i-search`ls': ls

You can press n times on “Ctrl+r” to see the results corresponding the term you are looking for.

reverse-i-search`ls': ls
reverse-i-search`ls': ls -a
reverse-i-search`ls': ls -l
reverse-i-search`ls': ls -al

See your history

The command is:

1
history

The output is (for example):

ls
ls -a
ls -l
ls -la

Episode 8 – Create a Folder in the Linux Shell Script Tutorial Directory

Link to the video

Create a directory

The command is:

1
mkdir -v myfolder

The output is:

mkdir: created directory ‘myfolder’

Then I can go to my new folder.
The command is:

1
cd myfolder

Create sub-folders at once

1
mkdir -p level1/level2/level3

The output is:

blank

The different levels of folders has been created. You can verify it.
The command is:

1
cd level1/level2/level3

Create multiple folders at once

1
2
mkdir folder1 folder2 folder3
ls

The output is:

folder1
folder2
folder3

Episode 9 – Moving Around the Linux Shell Script Tutorial Change Directory Folder

Link to the video

About

View all posts by

Leave a Reply

Your email address will not be published. Required fields are marked *