Bash Scripting in Linux

Mercy Jemosop
6 min readJun 28, 2022

--

Learning bash scripting and linux command for easy navigation.

Introduction

Bash scripting is a nice skill to learn to make your interaction with Linux system faster and easy.

Terminologies:

  • Bash is a command language interpreter widely available on many operating systems and It comes installed by default on the majority of GNU/Linux systems. ‘Bourne-Again SHell’ is the name’s acronym
  • Shell is a macro processor which allows interactive or non-interactive command execution. It is the command line interpreter, it takes commands from the keyboard and gives them to the operating system to execute.
  • Scripting allows for automatic command execution that would otherwise be executed interactively one by one.

Terminal window

The image above is a terminal in ubuntu. To open a terminal use the command CTRL+ALT+T. The terminal contains a shell which allows you to use commands to interact with your computer to: Retrieve/store data,Process Information etc.

Let’s check the basic linux commands ls(listing), pwd(present working directory), cd(navigation).

Terminal and Shell

Scripting

If you have a set of commands that you need to execute daily or after a period of time without fail. This will be a tedious process and can result in failure.

Scripting allows you to execute all commands at once. Let’s execute the above command in a script. We will use the vi editor to edit our script.

vi filename.sh- this create a new file and opens it in the vi editor. In my case I created a file named task. vi task.sh

N.B important commands to know in scripting

ESC- used to switch to command mode.

:wq -save and quit/exit(type all :wq ) on the bottom left. N.B you have to switch to the command mode before

To understand more about scripting commands

Enter all the commands we executed above using shell.

steps to flow

  1. vi filename.sh (opens the new file)
  2. Click i on your keyboard to move from command mode to insert mode.
  3. Type all your commands(if you cannot type that means you are in command mode, just type i to go to insert mode)
  4. Esc to go to command mode and type (:wq) save and exit or (:q!) force quit. This will close the window
  5. chmod +x filename.sh (give the file permission for the file to be executable. )
  6. ./filename.sh (execute the file)

give permission and run the script

As simple as that.

Bash

We have all used bash in some cases but we didn’t have knowledge of it. To see the default interpreter use the command

echo $SHELL

other shell interpreters include: korn shell, C shell etc. It is a good practice to define the shell interpreter to be used.

The command to define your script interpreter as bash

which- locate full path to its executable binary. eg which bash

which bash#output/usr/bin/bash

#!- this is a shebang which is used to prefix the full path of bash.

All your scripts will include the shell interpreter definition.

#!/bin/bash

File Name and Permissions

From the example above, when we created the new file task.sh. We needed to add the execute permission chmod +x. Newly created files are by default not executable regardless of their file extension suffix.

Attributes of Files in unix

  • Owner Permission, determines what action the owner of the file can perform on the file.
  • Group Permission, determine what action a user, who is a member of a group that a file belongs to, can perform on the file.
  • Other (world) permission, determine what action other users can perform on the file.

ls -l display various information related to file permissions.

The first column represent different access modes e.g permissions associated with a file or directory. Permissions are broken into group of threes and each position in the group denotes a specific permission, in this order: read(r), write(w) and execute(x)

  • The first three characters (2–4) represent the permissions for the file’s owner. -rwxr-xr — , in this example the owner has read (r), write (w) and execute (x) permission.
  • The second group of three characters (5–7) consists of the permissions for the group to which the file belongs. For example, -rwxr-xr — represents that the group has read (r) and execute (x) permission, but no write permission.
  • The last group of three characters (8–10) represents the permissions for everyone else. For example, -rwxr-xr — represents that there is read (r) only permission.

A breakdown example

  • - (rwx)owner (r-x)group (r- -)everyone

File Access Mode

There are three basic permissions

  • Read Permission,grants the capability to view a file.
  • Write Permission, grants the capability to modify or remove a file from a directory.
  • Execute permission, grants the capability to execute files and for directories it allows you to enter the directory.

We use the chmod(change mode) command to change the file or directory permission. There are two ways to use the chmod command symbolic and absolute mode.

With symbolic mode you can easily specify permission by using operators.

  • +, adds the designated permission(s) to a file or directory.
  • -, removes the designated permission(s) to a file or directory.
  • =, Sets the designated permission(s).

Chmod absolute permission

It uses a number to specify each set of permission for the file.

0, No permission, — — —

1, Execute permission , — — x

2, Write permission, — w —

3, Execute and write permission: 1 (execute) + 2 (write) = 3, — wx

4, Read permission,r— —

5,Read and execute permission: 4 (read) + 1 (execute) = 5,r — x

6, Read and write permission: 4 (read) + 2 (write) = 6, rw —

7, All permissions: 4 (read) + 2 (write) + 1 (execute) = 7, rwx

Navigating the File System

  • cat, displays a filename.e.g cat test.txt.
  • cd, Moves you to the identified directory.e.g cd projects.
  • cp,Copies one file/directory to the specified location.e.g cp file1 file2.
  • file,Identifies the file type (binary, text, etc) e.g file filename.
  • find,Finds a file/directory e.g file filename dir.
  • head, Shows the beginning of a file e.g head filename.
  • less, Browses through a file from the end or the beginning e.g less filename.
  • ls, Shows the contents of the directory specified e.g ls dirname.
  • mkdir ,Creates the specified directory e.g mkdir dirname.
  • more, Browses through a file from the beginning to the end e.g more filename.
  • mv, Moves the location of, or renames a file/directory e.g mv file1 file2.
  • pwd, Shows the current directory the user is in e.g pwd.
  • rm ,Removes a file e.g rm filename.
  • rmdir, Removes a directory e.g rmdir dirname.
  • tail , Shows the end of a file e.g tail filename.
  • touch , Creates a blank file or modifies an existing file or its attributes e.g touch filename.

Let’s apply this practically

  1. we create two files using the touch command
touch test 
touch test1

2. we check if out files were created using the ls command

3. You can type data in one of the file then use the cp command to copy content to another file.

Use the cat command to display data in the file

4. Create a directory using the mkdir command, move the two file to the new directory using the mv command. Move to the new directory using the cd command and check the contents on the directory using ls command.

You can explore more about scripting and linux commands.

sources:

tutorialspoint

--

--

Mercy Jemosop

Software Developer. I am open to job referrals. connect with me on twitter @kipyegon_mercy