Shell

A shell provides an interface for the operating system (CLI) and works as an interpreter to communicate with the system. In other words; a text based program for controlling thge operating system.
The basic operation involves gathering user inputs from the terminal (stdin) and executing the commands based on that.

There are multiple different shells available, bash, zsh, tcsh etc. Linux based operating systems always has atleast one installed "out of the box" but users can install additional shells aswell. In this course we focus on the most common one: bash

Shell scripting?

A shell script is a text file that contains a series of commands for a UNIX-based operating system.
Shell scripts shine at automating tasks on a operating system level. Anything you can do on the command line, can be executed in a script as well. Syntax supports the same basic programming procedures what every other language have: variables, conditionals, loops etc.
The script is initiated by executing the file on a command line.

If you need to regurarly perform tasks that involve the same commands, perhaps you should probably create a shell script for that task to save time?
What if the task must be executed at 2PM sharp? Will you woke up and go perform the commands or create a script for task and schedule a job to run it?

Of course you can perform same tasks using some other programming languages as well, but you need to install additional programs and libraries to your system.


Executing the script

As mentioned earlier, the shell script is actually just a text file. The file extension can be added to clarify what kind of file it is (usually .sh extension), but it is not required and you must always check whats inside!

Lets take a look a simple example.

Create a new file called script.sh. Remember that the file extension doesn't define anything!

#!/bin/bash
echo "My first script!"

output:
my first script!

If you want to execute script as a regular executable file, it must be set executable.
Change the file permissions to match your needs using a chmod command.

  • owner = u+x
  • owner group = g+x
  • others = o+x
  • all = a+x or +x
$ sudo chmod a+x script.sh

There are few ways to execute the script:

  • Directly typing path to the script /where/is/script (absolute path) and ./where/is/script (relative path). This makes the shell run the file as if it was a regular file and starts a new subprocess/interpreter where the script is executed. Note that the file must be executable and relative pathing has a slash / after the dot, it just separates name components.
  • Directly calling a desired interpreter followed by the path to file: bash /path/to/file. This style also starts a new subprocess/interpreter where the script is executed.
  • source command shortcut: dot and path to the file: . /path/to/file. The script is executed in the current shell session, which means that every definition/declaration will affect to it.
$ ./script.sh # this works if shebang on the first line
My first script!
$ bash script.sh # this works without shebang
My first script!

Sometimes you might need root permissions to perform wanted tasks like installing new programs, creating new users, removing other user things etc.
You can simply use sudo command in front of the execute command or switch to root user.


The Shebang

The first line of the script should always be the definations of the interpreter. If the defination is missing, the default interpreter will be used but it might be something else what you thought!

The first line starts with a #! annotation that is referred as a shebang, followed by the path to the wanted shell binary: /bin/bash. When shebang is defined, the interpreter is executed and the actual script is passed as an argument to the interpreter.

#!/bin/bash
ps -h -p $$

#/bin/bash ./example.sh
#!/bin/sh
ps -h -p $$

#/bin/sh ./example.sh
#!/bin/python3
print("Hello")

#Hello