Simple Bash Basics.

·

3 min read

Simple Bash Basics.

The shell is an important application that sits on top of the kernel and enables the user to interact with the operating system. The user gives commands to the shell which interacts with the kernel that gives those commands to the operating system.

It is important to realize that the shell is not the terminal. The terminal is a graphical window that lets the user interact with the shell, it is not the shell.

Shell

Navigating the shell is an important skill for anyone who uses computers especially with the GNU/Linux system. Mastering the command-line and how to manipulate the computer on the command-line is an important skill. This article goes over a few techniques with using the command-line using the Bash shell.

The shell works by accepting commands from the user and executing those commands. It displays a prompt and waits for the user input and of the input is not a command, it gives an error message and waits for the next command.

An example is the command to list files in a particular directory 'ls'.

Most accepted commands come with options that the user gives to specify the output of that command. For example with the ls command, options could be specified to list the files vertically, separated by commas, in long format etc.

This functionality is true for most commands that come with the shell.

Giving automatic commands to the shell

While manually typing commands is great and easier than using a mouse, it would be better if one could simply list commands that the shell could run by itself sequentially. In this case, one could simply list the commands in a file and run that file in the shell. For a file to be run, there are two ways:

  1. One way is by typing the command bash and giving the file as the input.

    When using this method, the file is written the way it's usually written with no additional changes and run in the command-line. An example is shown below.

    image of file script being run

  2. The other way is by writing the file with the shebang (#!/bin/bash) directive at the top and running the file from the current directory. The directive (#!) (shebang) tells the shell to run the proceeding lines with the program specified in the path following the shebang. In this case, since it is the bash shell, it gives the path to the bash program (/bin/bash).

    After the shebang, the file has to be made executable then it's path bis just specified. An example is shown:

    To change the mode, and make it executable, the chmod command is used. In this case the command is used as (chmod u+x filename).

    Applying the mode changes:

    The file can then be run by just specifying its path on the shell

These two ways are the most basic ways to run commands on the shell. Using both methods, it is possible to run scripts on the bash shell and automate simple tasks.