Linux Shell -Part 2

Linux Shell -Part 2

In this blog, I will be sharing about the shell, its types and the different commands and functions used in the shell.

Shell in Linux is the most powerful tool. From creating a directory, any file to executing complex programs is done from a shell only. Since I am using Ubuntu Linux, the default shell is BASH shell (i.e Bourne Again Shell)

We can list different shell types as:

  1. sh shell

  2. Bash shell

  3. Kshell (Korn shell)

  4. C shell (csh)

  5. tcsh shell and so on.

Different shells have unique features and abilities. We will be using the default shell i.e. Bash shell.

Shell Prompt

The normal/common user prompt is simply a dollar sign: "$" while the prompt for the root user is "#"

$ for regular user and # for root user

Running the basic commands

we can run the shell commands like:

$ date
$ whoami
$ ls

Most commands have one or more options that change the command's behavior and give the desired output. For example

Here we have used the ls command with options -a ,-l, -t

-a: shows the hidden files i.e. dot(.) (first file as in the above image.

-l: long listing(permissions, owner, group, date and so on)

-t: list by the time

Locating command

We can locate the location of the commands that we executed in the terminal. All these commands reside in the file somewhere but we don't know where it is. For example, date is located in /usr/bin/date directory. We can find the location of any directory using type or which.

Recalling Commands Using Command History

We can recall any command from history and edit the and restore back . We can do that by performing the history command.

Above is an example of the history command displaying 5 commands that we performed. We can see the unique number before any command. Applying "fc <unique id>" in the terminal the editor opens and changes the command and saves back and exit. In this way, we can recall any commands.

Command Line Command Completion

We can autocomplete any commands in the terminal using the TAB button.

For example: echo $OS<TAB>

Piping Between the commands ('|')

Pipe commands are used to pass the output of one command to another command which acts as an input.

$ cat /etc/passwd | sort | less

Here the output of /etc/passwd is passed to sort, where the content is alphabetically ordered and pipes the output to the less command.

Sequential Commands

Running multiple commands one after another in the same command line using semicolon(;). For example

SHELL VARIABLES

Shell variables are those that store the information which is useful for the users. Example: $SHELL -identifies the shell $USER- identifies the user

Aliases

It creates a shortcut to any command and options which can be easy to run. We can set the single character or word for a large path directory

alias p ='pwd; ls -CF'

Here, the letter p is assigned to run the command pwd and then to run ls -CF to print the current working directory and list its contents in column form.

To remove alias use unalias: unalias p

Getting information about commands

As we are new to Linux, we don't know which command to perform and which option to use. The beginner user needs detailed information about the commands and their outcomes on their execution. We can find the information using some options such as

  • help command: We can find lots of resources and ideas regarding any commands using the help command. The help command lists those commands and shows options available with each of them.

    syntax: help <command_name> eg: help date

  • --help command: many commands include --help option that we can use to get information about the command.

    Syntax: <command> --help

  • Man command: To learn more about a particular command, enter man <command>. eg: man ls

man page sections are maintained in order which can be summarized as

  • section 1 user command

  • section 2 system calls

  • section 3 c library functions device and special files

  • section 4 device and special files

  • section 5 file formats and Conventions

  • section 6 games

  • section 7 miscellaneous

  • section 8 system administration tools and Daemons

This is all for the shell . I hope you get the some ideas on how to use shell and some commands. Practice gives the best result. Keep practicing .

Happy Learning :)