Understanding the Basics of the Shell

Introduction

What exactly is the shell? The shell is a program that takes commands from the keyboard and gives them to the operating system to perform. Example: sh, zsh, ksh, tcsh etc. If that is the shell then what exactly is a terminal? A terminalis a program that opens a window and lets you interact with the shell. Example: bash, z shell, xonsh etc. The shell is like a medium of communication between the operating system and a user as it enables you to run commands a terminal sends the command to the operating system which in turn performs the operation. This mostly happens in the Command Line Interface.

Using shebang for scripts

This is a combination of pound key(#) t and exclamation mark (!) at the beginning of a script. It is also known as hashbang.

Example 1

#!/bin/bash

It is crucial to include that in a script so as to inform the interpreter what type of script it is. In the above example, we can say that it is a bash script. Other examples of scripts include Python scripts indicated as :

Example 2

#!/usr/bin/python3

Managing file permissions

Files in a Linux system are multi-user meaning they may or may not contain certain access rights. It contains three types of owners i.e. user, owner, and group. Rights can be assigned to read a file, write, and execute a file (i.e. run the file as a program).

The following commands are useful to make that possible:

  • chmod - modify file access rights

  • su/sudo - temporarily become a superuser

  • chown - change ownership

  • chrgp - change a file's group ownership

To see permissions for a file we use the command ls followed by the -l flag. Let's look at a bash file which is located in the /bin directory:

me@ubuntu~$ ls -l /bin/bash 
-rwxr-xr-x 1 root root 1183448 Apr 18  2022 /bin/bash

In the above example:

  • - - indicates its a file for a directory it is indicated by d

  • rwxr - indicates that a file's owner/user may read, write and execute the file. All other groups may only read and execute the file.

  • xr - The group can execute and read the file

  • x - Others can only execute the file

The characters represent:

  • r = read permission

  • w = write permission

  • x = execute permission

  • = no permission/ excluding the first one to appear

Changing mode (chmod)

We can use the chmod command which stands for ‘change mode’ to change permissions of a file or directory. Using the command, we can set permissions (read, write, execute) on a file/directory for the owner, group, and the world.

Syntax:

chmod permissions filename

Here is a table of numbers that covers all the common settings:

ValueMeaning
777(rwxrwxrwx) No restrictions on permissions. Anybody may do anything. Generally not a desirable setting.
755(rwxr-xr-x) The file's owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users.
700(rwx------) The file's owner may read, write, and execute the file. Nobody else has any rights. This setting is useful for programs that only the owner may use and must be kept private from others.
666(rw-rw-rw-) All users may read and write the file.
644(rw-r--r--) The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change.
600(rw-------) The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private.

The following numbers represent permissions as follows"

  • rwx : 111 in binary = 7

  • rw : 110 in binary = 6

  • r-x : 101 in binary = 5

  • r-- : 100 in binary 4

To get those numbers we use this as a guide:

  • (No Permission) => 0

  • r(Read) => 4

  • **w(Write) => 2

  • **x(Execute) => 1`

Using this we can now figure out what the commands do. For example:

chmod 755 some_file

The above command gives the file's owner rights to read, write, and execute the file. All others may read and execute the file.

Changing ownership (chown)

We can change the owner of a file by using the chown command.

If we wanted to change the owner of some_file from "me" to "you". We could:

sudo chown you some_file

Note: To change the owner of a file, we must have superuser privileges. To do this, our example employed the sudo command to execute chown.

chown works the same way on directories as it does on files.

Changing group ownership (chgrp)

The group ownership of a file or directory may be changed with chgrp. This command is used like this:

chgrp new_group some_file

In the example above, we changed the group ownership of some_file from its previous group to "new_group". We have to be the owner of the file or directory to perform a chgrp.

Conclusion

As you continue your journey into the realm of command-line operations, remember that mastery of these fundamental concepts is key to unlocking a world of possibilities within the vast landscape of shell scripting and system administration.

Happy scripting, and may your commands always run swiftly and accurately!