Skip to main content
Engineering LibreTexts

13-B.3: Fundamentals of Linux Scripts

  • Page ID
    43188
  • EXAM OBJECTIVES COVERED
    5.1 Given a scenario, deploy and execute basic Bash scripts.

    Shells

    There are several shells available for Linux systems. Each shell does the same job but understands different commands and provides different built-in functions.

    • BASH (Bourne Again SHell) – The most widely used shell in Linux systems. It is used as default login shell in Linux systems and in macOS. It can also be installed on Windows OS.
    • csh (C SHell) – The C shell’s syntax and usage are very similar to the C programming language.
    • ksh (Korn SHell) – The Korn Shell also was the base for the POSIX Shell standard specifications, etc.
    • tcsh – An enhanced version of csh, the C shell.
    • zsh – A powerful interactive shell.
    • scsh – An open-source Unix shell embedded within Scheme programming language.

    In this discussion we will be using the BASH shell for all examples since it is the default shell of most Linux distributions.

    Shell Scripting

    Usually shells are interactive, which means the shell accepts commands as input from users and executes them. However, there are times it is necessary to execute a group of several commands routinely, so we must type each command into the terminal.

    A shell can accept commands as input from a file, and these commands can be written into a text file which can then be executed to avoid the repetition of having to type each command numerous times. These files are called shell scripts or shell programs, or simply scripts. By tradition, each shell script is saved with .sh file extension e.g., myscript.sh

    A shell script has syntax just like any other programming language. If you have any prior experience with any programming languages like Python, C/C+,+ etc. it would be very easy to get started with shell scripts.

    A shell script is normally comprised of the following elements:

    • Shell Keywords – if, else, break, etc.
    • Shell commands – cd, ls, echo, pwd, touch, etc.
    • Functions
    • Control flow – if..then..else, case and shell loops, etc.

    Why Do We Need Shell Scripts

    There are many reasons to write shell scripts:

    • To avoid repetitive work and automate tasks
    • System admins use shell scripting to create routine sets of commands
    • System monitoring
    • Adding new functionality to the shell, etc.

    Advantages of Shell Scripts

    • The command and syntax are exactly the same as when entered directly on the command line, so programmers use the familiar command syntax.
    • Writing shell scripts is quicker than writing in a typical programming language (C++, Java).
    • Quick start
    • Interactive debugging, etc.

    Disadvantages of Shell Scripts

    • Prone to costly errors, a single mistake can change the command which might be harmful.
    • Slow execution speed.
    • Design flaws within the language syntax or implementation.
    • Not well suited for large and complex tasks.
    • Provide minimal data structure unlike other scripting languages, etc.

    Variable Substitution

    The name of a variable is a placeholder for its value, the data it holds. Referencing (retrieving) its value is called variable substitution.

    pbmac@pbmac-server $ variable1=23
    
    pbmac@pbmac-server $ echo variable1
    variable1
    
    pbmac@pbmac-server $ echo $variable1
    23

    Let us carefully distinguish between the name of a variable and its value. If variable1 is the name of a variable, then $variable1 is a reference to its value, the data item it contains.

    The only times a variable appears "naked" -- without the $ prefix -- is when declared or assigned, when unset, when exported, in an arithmetic expression within double parentheses (( ... )), or in the special case of a variable representing a signal (see Example 32-5). Assignment may be with an = (as in var1=27), in a read statement, and at the head of a loop (for var2 in 1 2 3).

    Enclosing a referenced value in double quotes (" ... ") does not interfere with variable substitution. This is called partial quoting, sometimes referred to as "weak quoting." Using single quotes (' ... ') causes the variable name to be used literally, and no substitution will take place. This is full quoting, sometimes referred to as 'strong quoting.'

    Note that $variable is actually a simplified form of ${variable}. In contexts where the $variable syntax causes an error, the longer form may work (see Section 10.2, below).

    Example 4-1. Variable assignment and substitution

    #!/bin/bash
    # ex9.sh
    
    # Variables: assignment and substitution
    
    a=375
    hello=$a
    #   ^ ^  - NOTICE there are NO spaces on either side of the = sign....

    Variable Assignment

    The assignment operator (no space before and after):

    Warning emoji

    Do not confuse this with = and -eq, which test, rather than assign!

    Note that = can be either an assignment or a test operator, depending on context.

    Example 4-2. Plain Variable Assignment

    #!/bin/bash
    # Naked variables
    echo
    
    # When is a variable "naked", i.e., lacking the '$' in front?
    # When it is being assigned, rather than referenced.
    # Assignment
    a=879
    echo "The value of \"a\" is $a." 
    # NOTICE - the \ character escapes the following" when we want to use quotations within another set of quotations marks.
    
    # Assignment using 'let'
    let a=16+5
    echo "The value of \"a\" is now $a."
    

    Adapted from:
    "Introduction to Linux Shell and Shell Scripting" by Atul Kumar, Geeks for Geeks is licensed under CC BY-SA 4.0
    "4.1. Variable Substitution" by Multiple Contributors, The Linux Documentation Project is licensed under CC BY-SA 3.0
    "4.2. Variable Assignment" by Multiple Contributors, The Linux Documentation Project is licensed under CC BY-SA 3.0

    • Was this article helpful?