A little introduction to BASH (Born Again Shell)

A quick introduction to the very basics starting from the UNIX operating system.




This is some hints about shell script programming based on examples. I provide here some little scripts which will hopefully help to understand the very basics.

The shell or terminal is a command line interpreter.

A command is a set of instructions or computer tasks.

The interest to work with command line instead of an user graphical interface is to automate a computer task or a complex set of tasks.

Files, directories, symbolic links

The file or more accurately the name of the file is a pointer. A pointer stores memory address on the hard drive from where the file can be read.

It is possible to read, write or execute a file. To do these actions, you need to provide the pointer i.e. the name of the file.

The directory is also a pointer but not pointing at a file. Actually the directory point at a list of other pointers. A directory "contains" (more accurately "point at") files (more accurately "names of files"). As a directory is also a pointer, a directory can contain others directories.

Symbolic links are pointers which are pointing at other pointers. Broadly, they are copies of pointers. To use a symbolic link remains the same as using the name of a file/directory at which it points.

Browse files and directories

Change Directory cd

To change the current working directory to a specified one.

Commands

Switch to the directory root. (This is the last parent directory)

cd /

Switch to the directory home. (This is the directory reserved for the user)

cd ~

OU

cd /home/user

Switch to the current working directory (This command is useless because you are already working on this directory)

cd .

Switch to the parent directory. (very useful)

cd ..

Switch you to the previous directory

cd -

Put you in a subdirectory called mon_dossier.

cd mon_dossier

Switch to the sub-subdirectory called mon_dossier_dans_le_dossier which is in the subdirectory called mon_dossier.

## 2 commands
cd mon_dossier
cd mon_dossier_dans_le_dossier
## single command
cd mon_dossier/mon_dossier_dans_le_dossier

Concept of absolute path and relative path

Frequently, one begin to use the shell (and even after), most of errors comes from wrong path. (using the relative path is less daunting but more perilous.)

When you type path to a file, speeds up it by using autocompletion with the keypad TAB.

Print name of Working Directory pwd

To give you the absolute path(i.e. the path from root) of current working directory.

Command

pwd

Output

/the/directory/where/i/am

I'm currently working into the directory /the/directory/where/i/am

List ls

To give you the list of names of files and directories which are located into the designated directory. (by default, the designated directory is your current working directory)

Command

ls

Output

file_into_my_current_dir.txt
ls my_dir

Output

file_into_my_dir.txt

To display long format list of my_dir content

ls -l my_dir

Output

-rwxrw-r-- 1 user teamBI 697 jan. 23 11:09 file_into_my_dir.txt

-l option displays details of files located into the directory called my_dir.
-rwxrw-r--
- rwx rw- r--

For instance here, my file(-) file_into_my_dir.txt can be read and written and executed by the user user (first triplet: rwx) who is the owner of this file. People who belongs to the group teamBI can also read and write but no execute this file (second triplet: rw-) and foreigners can only read the file (last triplet: r--). This file size is 697 bytes. This file was created january the 23th at 11:09.

Some useful commands

Manual man

Print the manual of a shell program

Commmand

man ls

Print the first 15 rows of a file.

Command

head mon_fichier

Will print the first 15 rows of the file mon_fichier into the shell.

tail

Print the last 15 rows of a file.

Command

tail mon_fichier

Will print the last 15 rows of the file mon_fichier into the shell.

File viewing more

To view the content of a file into the shell. more is especially primitive.

Command

more mon_fichier

Will show the content of mon_fichier into the shell.

Advanced file viewing less

Display the content of a file into a session with more features than more.

Command

less mon_fichier

Will show the content of mon_fichier into an instance.

Text editor vi

To read, write and edit a file a bit like a notepad in windows. They are a lot of text editor under LINUX: my favorite is vi (vim is its improved version), nano is also very popular.

Command

vi mon_fichier

Will load a text editor session to edit mon_fichier. By default, vi only allow you to read a file.

You must exit insert mode to execute commands which are not text typing.

Note : if mon_fichier don't exist at the moment I enter the command, then vi will create this file.

echo

This command is useful associated with other commands because it returns its input as a text which can be interpreted by the shell.

Command

To return a shell string "coucou".

echo “coucou”

Output

coucou

Autre exemple

To return the name of a file mon_fichier as a string.

echo mon_fichier

Output

mon_fichier

Concatenate cat

To concatenate files together.

Command

cat mon_fichier

Will show content of mon_fichier into the shell.

Concatenate 2 files together

Merge and print contents of mon_fichier1 and mon_fichier2 into the shell.

cat mon_fichier1 mon_fichier2

Merge and print contents of mon_fichier2 and mon_fichier1 into the shell.

cat mon_fichier2 mon_fichier1

Word Count wc

Print number of words into a file.

Command

wc mon_fichier

Output

1    2    3 mon_fichier

Will print the number of rows (newline), number of words and number of characters for the file mon_fichier. Here, mon_fichier counts 1 row, 2 words and 3 characters. (a first word with 1 character and a second word with 2 characters.)

Count lines for a file

The option -l print only the newline counts.

wc -l mon_fichier

Output

The mon_fichier counts only one line.

1

Count words for a file

The option -w print only the word counts.

wc -w mon_fichier

Output

2

Count characters for a file

The option -c print only character counts.

wc -c mon_fichier

Output

3

Combine commands together

Most of LINUX programs are designed under the principle "Do one thing and do it well". For instance, ls print list of files into a directory and wc counts number of newlines or words for an input.

We can resume a program as something as basic as:

input → program → output

For instance :

ls .

ouput :

fichier1 fichier2 fichier3

This command prints a list of 3 files into my current working directory.

Now let's combine 2 programs together using |
ls . | wc -w

ouput :

3

note: ; set the end of a command. You can use indifferently newlines or ;.

For example
echo “coucou” > coucou.txt ; more coucou.txt

output :

coucou

I wrote the output of echo inside a file called coucou.txt. Next, I print the content of this file coucou.txt with more.

Continued from example
cat coucou.txt | wc -c > nombre_carac_coucou.txt
cat coucou.txt nombre_carac_coucou.txt > coucou_nombre.txt
more coucou.txt

output :

coucou
more nombre_carac_coucou.txt

output :

7
more coucou_nombre.txt

output :

coucou
7

note: why 7 characters for "coucou" which actually counts 6 characters ? Well, it is because the program also counted the jumpline as a character. Indeed, I wrote "coucou" but i redirected the output of echo and echo added a new line (it's nicer). So, echo sent to wc the character string coucou\n (with \n the jumpline character).

If you are a perfectionnist, add the option -n to echo:

echo -n "coucou" | wc -c

Thus, echo will not add a jumpline and wc -c will count 6 characters.

This is an example to tell you to remain vigilant about your results, and the behavior of your programs. Take the time to understand them well in order to pick the most appropriate program for what you want to do.