Saturday, June 9, 2018

Linux Shell


Kinds of Shells
---------------

bash
  - Bourne Again SHell
  - based on earlier version of sh
  - commonly default shell in Linux

sh
  - Bourne shell
  - this is were BASH was based
  - not often used in Linux but usually a pointer to /bin/bash

tcsh
  - based on earlier C shell (csh)
  - not popular in Linux

csh
  - the original C Shell
  - not commonly used in Linux
  - very similar to tcsh

ksh
  - combination of features of csh and sh

zsh
  - Z Shell
  - evolution of ksh

How to start a shell?
--------------------

There are several ways and here are the most common ways.

1. Once you logged in to a linux server (either by SSH or via console), you
   automatically starts your own shell
2. In xterm or GUI, you can right click on the desktop and open a terminal. In
   that way, you are also starting a shell

Parts of a shell
----------------

Understanding every part of the shell is very important specially for linux
administrators because it is the one you interact with everytime you deal with
Linux.

As an example, when you logged as a regular, the shell will provide you a prompt
where you can type your commands.

$ uname
Linux
$

The "$" represents something. In Linux by default, it means that you are logged
in as regular user. You may customized it by changing the PS1 environment
variable.

A root's prompt starts with "#" so its important to clearly distinguished a
regular user's prompt from a root's prompt. That can avoid accidentally running
destructive command like "rm -fr *" on /. The command used in the example above
is "uname" and its output is "Linux". Depending on the command you type, it may
return an output or nor. Or it may even return an error.

Types of commands you can use
-----------------------------

2 types of commands are "Internal" and "External" commands. Internal are the
ones built-in in linux while External are the  ones came from outside sources
(e.g came from installing a package). To see what type
of command, execute the following:

$ type cd
cd is a shell builtin
$ type date
date is /bin/date
$
$ type -a pwd
pwd is a shell builtin
pwd is /bin/pwd
$

The first example tells us that "cd" is an Internal command while the second one
indicates that "date" is an external command. There are some commands you may
encounter that has both Internal and External versions.

So to see it, add "-a" on type command that shown in the 3rd example above.

Keeping Track of Command History
--------------------------------

- bash history displays previous commands (usually last 500)
- this doesn't record the commands you enter inside of a prompt as:
   [root@localhost ~] passwd  # this will appear in bash history
   Changing password for user root.
   Changing password for root.
   Current password:          # anything you type here will not

history  # displays typically last 500 lines
history -c  # clears current history
~/.bash_history  # this is your history file (be aware that this might be
readable by other users)

Ways of executing a program
---------------------------

1. Using "exec"
  - run by: exec /path/to/program
  - exec doesn't create a new process
  - exec replaces the current shell
  - when the process terminate, your shell also terminates

2. The most common way
  - to run a script this way: ./myscript.sh
  - or this way: /path/to/myscript.sh
  - the script must have executes bit enabled (chmod +x myscript.sh)

Shell Tricks
------------

Typing in linux terminal is a major part in using a Linux based system so
here are some shell tricks and shortcuts that makes typing easier and more
efficient.

tab completion
 - you may hit TAB at the end of each command to auto complete
 - if the amount of string entered doesn't allow bash to determine uniqueness,
   you may
   hit another TAB to display possible results (so it would be TAB..TAB)
 - further improved by installing bash-completion

examining previous commands entered
  - typing "history" will show past entries accompanied with a number
  - given the history number, you may type ! to re-execute a specific
    entry which
    is helpful in re-typing long commands such as "for i in blah blah..; do
    blah ... blah
  - another efficient way of searching is by using "reverse search"; that is by
    pressing CTRL + R then you may start searching any keyword at the start,
    middle, or end of the command

fast moving
  - pressing ALT + F (or CTRL + right arrow in some) will move the cursor
    position 1 word forward while ALT + B to move 1 word backward (CTRL + left
    for some)
  - to quickly move at the end of the line, you may press CTRL + E (or just hit
    END button) on your keyboard while moving at the start is CTRL + A (or just
    HOME button)

delete multiple words at a time
  - by default pressing backspace (or CTRL + H) will delete 1 character at a
    time
  - to delete 1 word at a time, press CTRL + W
  - pressing CTRL + U will delete all words before the current cursor position
  - CTRL + K will delete all words after the current cursor positi

getting the last word of previous command (one of my favorite)
  - often we need to use the last word of the previous command we entered
  - as an example, see the series of commands below:
      touch /tmp/test
      chmod 600 /tmp/test
      vi /tmp/test
  - to quickly reuse /tmp/test instead of re-typing it or copying it, you may
    press ESC followed by . (period)

Terminating the shell
---------------------

Two commands can be used to terminate a shell session:

exit - terminates any shell
logout - same as above but terminates only login shells (e.g logins from ssh)

Getting help
------------

Since we are dealing with command line, we often need more information on a
particular command we are using. There are several ways on how to display
command information.

1. Using "man" - perhaps the most important help utility in Linux
  man   # displays man pages of a command
  man -k   # searches the man database for commands (or description)
                     matching the specified string
  man -K   # similar as above but do a full search (takes significantly
                     longer to complete)
  man
  # prints information on a specific
                                                manual section (see discussion
                                                below)

Man contains sections which groups the type of information you can see on a
particular command. Not all command have the same manual sections. Here are the
example of manual sections:

Section Number
  1 -- user commands (contains traditional command usage and options)
  2 -- system calls provided by the kernel (not commonly used by sys ads)
  3 -- library calls
  4 -- device files
  5 -- file formats (these are config files like "man 5 fstab")
  6 -- games
  7 -- miscellaneous
  8 -- system administration commands (usually ones that must be run as root)
  9 -- kernel routines

On the 9 manual sections, only 1, 5, and 8 are the most commonly used by system
administrators.

2. Using pinfo/info - not all commands support this
  info coreutils 'ls invocation'  # as an example, you can see this command
  under "SEE ALSO" of ls (1)

3. Exploring /usr/share/doc
  - some packages includes README files under this directory
  - you can see examples here
  - sometimes there are also sample configurations

Environment Variables
---------------------

These are global values read by the shell and inherited by the user (which can
also be overriden) during startup. To see all environment variables, type "env".
Here are the common environment variables in Linux.

PS1 - this is your prompt (e.g [user@host] $)
HOME - contains path to your home directory
HOSTNAME - the hostname of current machine where you are logged in
LOGNAME - the username used to login to the machine

To display the values, use the following command:

echo $HOME

sources:

PS1 code generator | http://bashrcgenerator.com/

No comments:

Post a Comment