Linux - Getting Started With Shell Programming

Introduction to BASH

    Developed by GNU project.
    The default Linux shell.
    Backward-compatible with the original sh UNIX shell.
    Bash is largely compatible with sh and incorporates useful features from the Korn shell ksh and the C shell csh.
    Bash is the default shell for Linux. However, it does run on every version of Unix and a few other operating systems such as ms-dos, os/2, and Windows platforms.

Quoting from the official Bash home page:

Bash is the shell, or command language interpreter, that will appear in the GNU operating system. It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use. In addition, most sh scripts can be run by Bash without modification.
The improvements offered by BASH include:

The Bash syntax is an improved version of the Bourne shell syntax. In most cases Bourne shell scripts can be executed by Bash without any problems.

    Command line editing.
    Command line completion.
    Unlimited size command history.
    Prompt control.
    Indexed arrays of unlimited size (Arrays).
    Integer arithmetic in any base from two to sixty-four.
    Bash startup files - You can run bash as an interactive login shell, or interactive non-login shell. See Bash startup files for more information.
    Bash conditional expressions: Used in composing various expressions for the test builtin or [[ or [ commands.
    The Directory Stack - History of visited directories.
    The Restricted Shell: A more controlled mode of shell execution.
    Bash POSIX Mode: Making Bash behave more closely to what the POSIX standard specifies.

Bash v4.0 Features

    Usual run time environment: POSIX
    Command and file name completion - Bash can automatically fill in partially typed commands or arguments to the commands such as file name, hostname and much more.
    Arithmetic support:
        Integer arithmetic supported.
        Floating point arithmetic is not supported.
        Exponential notation is limited via printf builtin.
        Date and time arithmetic is not supported.
    Hash table: Bash uses a hash table to remember the full pathnames of executable files.
    Pattern Matching and regular expressions are supported.
    Globbing - For example, you can use *.conf to match all those conf files in /etc directory.
    Directory stack is supported via pushd and popd builtins.
    Command history and History completion fully supported by Bash.
    Custom command prompt - Allows you to change the default prompt.


Bash and Command Types

The bash shell understands the following types of commands:

    Aliases such as ll
    Keywords such as if
    Functions (user defined functions such as genpasswd)
    Built in such as pwd
    Files such as /bin/date

The type command can be used to find out a command type.
type command

The type command can be used to find out if a command is built in or an external binary file.
Find out if ls is built in or an external command

Type the following command at a shell prompt:

type -a  ls

Sample Output:

ls is /bin/ls

To find out if history command is built in or an external command, enter:

type -a  history

sample Output:

history is a shell built in

However, some commands are supplied as both internal and external commands. For example:

type -a true
type -a echo

sample Outputs:

echo is a shell built in
echo is /bin/echo

List of command bash keywords and built in commands

    JOB_SPEC &
    (( expression ))
    . filename
    [[:]]
    [ arg... ]
    expression
    alias
    bg
    bind
    builtin
    caller
    case
    command
    compgen
    complete
    continue
    declare
    dirs
    disown
    echo
    enable
    eval
    exec
    exit
    export
    false
    fc
    fg
    for
    getopts
    hash
    help
    history
    if
    jobs
    kill
    let
    local
    logout
    popd
    printf
    pushd
    pwd
    read
    readonly
    return
    select
    set
    shift
    shopt
    source
    suspend
    test
    time
    times
    trap
    true
    type
    typeset
    ulimit
    umask
    unalias

    unset
    until
    variables
    while
Shell is used for various purposes under Linux. Linux user environment is made of the following components:

    Kernel - The core of Linux operating system.
    Shell - Provides an interface between the user and the kernel.
    Terminal emulator - The xterm program is a terminal emulator for the X Window System. It allows user to enter commands and display back their results on screen.
    Linux Desktop and Windows Manager - Linux desktop is collection of various software apps. It includes the file manger, the windows manager, the Terminal emulator and much more. KDE and Gnome are two examples of the complete desktop environment in Linux.

Login

User can login locally into the console when in runlevel # 3 or graphically when in runlevel # 5 (the level numbers may differ depending on the distribution). In both cases you need to provide username and password. Bash uses the following initialization and start-up files:

    /etc/profile - The systemwide initialization file, executed for login shells.
    /etc/bash.bashrc - The systemwide per-interactive-shell startup file. This is a non-standard file which may not exist on your distribution. Even if it exists, it will not be sourced unless it is done explicitly in another start-up file.
    /etc/bash.logout - The systemwide login shell cleanup file, executed when a login shell exits.
    $HOME/.bash_profile - The personal initialization file, executed for login shells.
    $HOME/.bashrc - The individual per-interactive-shell startup file.
    $HOME/.bash_logout - The individual login shell cleanup file, executed when a login shell exits.
    $HOME/.inputrc - Individual readline initialization file.

Bash Startup Scripts

Script of commands executed at login to set up environment. For example, setup JAVA_HOME path.
Login Shell

Login shells are first shell started when you log in to the system. Login shells set environment which is exported to non-login shells. Login shell calls the following when a user logs in:

    /etc/profile runs first when a user logs in runlevel # 3 (the level numbers may differ depending on the distrib

    ution).
        /etc/profile.d
    $HOME/.bash_profile, $HOME/.bash_login, and $HOME/.profile, runs second when a user logs in in that order. $HOME/.bash_profile calls $HOME/.bashrc, which calls /etc/bashrc (/etc/bash.bashrc).

Non-Login Shell

    When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc or /etc/bashrc and $HOME/.bashrc, if these files exist. First, it calls $HOME/.bashrc. This calls /etc/bash.bashrc, which calls /etc/profile.d.

Bash Logout Scripts

    When a login shell exits, bash reads and executes commands from the file $HOME/.bash_logout, if it exists.

Source: https://bash.cyberciti.biz/guide/The_role_of_shells_in_the_Linux_environment