Phase 1: Bash
Table of contents
Hello everyone! ๐
I started phase 1 of my cloud study by learning about bash, its fundamentals, bash scripting, and its usefulness in cloud computing. I will start by giving brief explanations of a few terminologies I learned, and a run-through of the course. At the end of this write-up would be a list of some books that aided me in my study.
What is a kernel?
A kernel is more like the heart of the computer, It is the piece of software that tells the computer what to do.
What is a shell?
A shell is a command language interpreter. It allows you to control your computer with commands from your keyboard. The shell serves as a way to communicate with the kernel.
You must be wondering ๐ค โWhy use the shell when you can easily make use of the GUI-Graphical User Interfaces?โ. From my experience so far, Iโll say the shell is fun and interactive. I see myself wanting to do more stuff with it.
What is a terminal?
A terminal is a program that gives you access to the shell.
Just like if you need access to your car, you would have to open it with the keys. The same goes for the terminal.
What is BASH?
BASH stands for Bourne-again Shell. Bash is the most widely used shell. Besides Bash, there are other widely used shell programs such as zsh, ksh e.t.c
What is a shell script?
A shell script is basically standard inputs or commands that are put into a file. Instead of interacting directly with the shell to run all your commands, you simply use a script, embed all your commands and run it. The output is the same as running the commands individually.
Basic commands to be familiar with
pwd
- present/print working directory
Letโs say you are in a directory or file and you donโt know where exactly you are, this command prints out the pathway that leads you to your present file or directory.
cd
- change directory
Added to this command is usually the directory you want to navigate to. It lets you change your current directory. Example; cd ~
(This takes you to your home directory).
cd ..
- This command also lets you move from your current directory to your former directory. For example; let's say you were in /etc/apt/keyrings
and you want to leave the /keyrings
directory to the /apt
directory, all you need to do is input the cd ..
command.
ls
- listing
This command lists all the files or directories in a directory.
ls -la
- This is another form of listing, only that it lists all the hidden files that are in that directory.
whoami
- This command shows you which user you are logged in as.
โhelp
or -h
or -?
- This command placed after the command you would want to inquire about shows documentation of that particular command.
man
- manual
The man command shows the manual of the command you would like to inquire about.
mkdir
- make a directory
This command followed by the name you would like to give your new directory creates a new directory for you.
rm
- remove
This command followed by the name of the file or directory deletes it from your system.
cp
- copy
As the name implies, this command copies the content of a file to another location.
cat
- concatenate
The cat command followed by the filename reads the content of the file. This command can also be used to create a file when used in this manner cat > [filename]
.
touch
- This command followed by the name of a new file, creates a new file for you.
grep
- Global Regular Expression Print
Let's say the folder you are in is extremely long and you are looking for a particular word, the grep command followed by the word filters through the documentation for that word and returns it to you.
chmod
- change mode
This command lets you change the permissions of the file.
nano
/vim
/mousepad
- These are all text editors. They are used to type scripts.
Bash Scripting
As I said earlier, a bash script is a series of commands that are put together in a file, so instead of typing directing on the command line, the commands are arranged in the file in the order you want it to execute. The output is pretty much the same as though you were to type it directly.
How are scripts executed?
To execute or run a script, you have to ensure that the permission of the user is set to x-execute. You can check the permissions of a file by running the ls -la
command. If the user does not have the execute command, you can set it by using the chmod
command. To change the mode of the user, run this command chmod 744 (filename)
. This command sets the user to have read(r), write(w) and execute(x) permissions while the group and others have just the read permission.
What makes up a script?
A bash script is made up of; shebang, variables, functions, conditions, comments, arguments, logical expressions, arrays e.t.c
- A script always starts with what is known as shebang i.e the #! followed by the path to the interpreter. The bash shell in this case is our interpreter since our focus is bash scripts, so it is
#!/bin/bash
.
A comment is a note to yourself or any other person or developer that might come in contact with your script. It should state the purpose of the script and any other side note you wish to add. It should always start with a
#
or else the system would see its content as part of what it is to execute.A variable holds a value. Take for example what you want to type is long or you are bound to forget it, you can store it in a variable. A variable outside a function is known as a global variable, and it can be called at any point in your script. When you just want the variable to have an effect only in the function, you add the word โlocalโ before you type your variable in the function.
You use an array when you have lots of things, values, or names you wish to place in one variable. You can access each input by using a dollar sign
$
followed by the number of the value.A function is more like a code within a code or a script within a script. Amazing right ๐ you can also call a function multiple times within your script. A function can contain several values and arguments.
There are lots of documentation about bash scripting, I recommend you take your time to go through any of your choices for deeper understanding.
Executing your script
The best convention is to always place your scripts in one directory, preferably the bin directory in your home directory for easy location. To avoid using the execute command ./
next to your filename when you want to run a file, it is best to add the bin directory that contains all your script in your home directory to your PATH
variable.
You can run $PATH
in your command line to view the values/paths given to the PATH variable. You can choose to add your bin directory temporarily or permanently. If you choose to add it temporarily you can run PATH=$PATH :/~/bin
this command adds the directory temporarily. If you choose to add the directory permanently all you need to do is edit the .bashrc
or .profile
files in your text editor by adding the same command at the end of the file.
Once youโve added your path to the PATH
variable, this tells the system to look into your directory when it is running a file. Therefore, you no longer need to execute your commands by typing the ./
before your filename again, all you need to do is type in your filename and it runs. Easy and better off right ๐
For a detailed look at more commands and bash scripting and how they function, here is a list of books and resources that assisted me.
Linux Basics for hackers
The Linux command line.
# My comments to you ๐
# Procastination is bad ๐ I've been postponing this write-up for some time now, kept saying I'll do it but ended up shifting it till now.
# I'm glad I got to do it!
# I tried as much as I can to keep the write-up as brief as possible so it doesn't get boring to read ๐.
# If you've gotten to this point YOU ARE THE BEST!
# DON'T DOUBT YOURSELF YOU ARE DOING GREAT ๐
Subscribe to my newsletter
Read articles from Melissa Frank directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Melissa Frank
Melissa Frank
I am a lover of tech majoring in cloud computing, and exploring emerging opportunities in Web3, Mechine learning and AI. Feel free to interact with me on anything related to tech ๐