Skip to main content
Engineering LibreTexts

13-C.6: Writing and Running Linux Scripts

  • Page ID
    43194
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    EXAM OBJECTIVES COVERED
    5.1 Given a scenario, deploy and execute basic Bash scripts.

    What is shebang?

    The shebang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script. It is also called sha-bang, hashbang, pound-bang, or hash-pling.

    When a text file with a shebang is used as if it is an executable in Linux, the program loader mechanism parses the rest of the file's initial line as an interpreter directive. The loader executes the specified interpreter program, passing to it as an argument the path that was initially used when attempting to run the script, so that the program may use the file as input data.

    The fun begins when we start writing out own shell scripts, a file that contains a mix of Linux commands and programming concepts. This video is the beginning of the shell scripting journey.

    Shell Metacharacters

    Metacharacters are special characters that are used to represent something other than themselves. As a rule of thumb, characters that are neither letters nor numbers may be metacharacters. Like grep, sed, and awk, the shell has its own set of metacharacters, often called shell wildcards.

    Symbol Meaning
    > Output redirection
    >> Output redirection (append)
    < Input redirection
    * File substitution wildcard; zero or more characters
    ? File substitution wildcard; one character
    [ ] File substitution wildcard; any character between brackets
    `cmd` Command substitution
    $(cmd) Command substitution
    | The Pipe (|)
    ; Command sequence, sequences of commands
    [ ] File substitution wildcard; any character between brackets
    || OR conditional execution
    && AND conditional execution
    ( ) Group commands, sequences of commands
    & Run command in the background, background processes
    # Comment
    $ Expand the value of a variable
    \ Prevent or escape interpretation of the next character
    << Input redirection

    Exit Codes

    The exit command terminates a script, just as in a C program. It can also return a value, which is available to the script's parent process.

    Every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code. Well-behaved UNIX commands, programs, and utilities return a 0 exit code upon successful completion, though there are some exceptions.

    Likewise, functions within a script and the script itself return an exit status. The last command executed in the function or script determines the exit status. Within a script, an exit nnn command may be used to deliver an nnn exit status to the shell (nnn must be an integer in the 0 - 255 range).

    When a script ends with an exit that has no parameter, the exit status of the script is the exit status of the last command executed in the script (previous to the exit).

    #!/bin/bash
    COMMAND_1
    . . .
    
    COMMAND_LAST
    # Will exit with status of last command.
    
    exit
    

    The equivalent of a bare exit is exit $? or even just omitting the exit.

    #!/bin/bash
    COMMAND_1
    . . .
    
    COMMAND_LAST
    
    # Will exit with status of last command.
    exit $?
    
    #!/bin/bash
    COMMAND1  
    . . .   
    
    COMMAND_LAST   
    # Will exit with status of last command.

    $? reads the exit status of the last command executed. After a function returns, $? gives the exit status of the last command executed in the function. This is Bash's way of giving functions a "return value."

    Following the execution of a pipe, a $? gives the exit status of the last command executed.

    After a script terminates, a $? from the command-line gives the exit status of the script, that is, the last command executed in the script, which is, by convention, 0 on success or an integer in the range 1 - 255 on error.

    #!/bin/bash
    
    echo hello
    echo $?    # Exit status 0 returned because command executed successfully.
    
    lskdf      # Unrecognized command.
    echo $?    # Non-zero exit status returned -- command failed to execute.
    
    echo
    exit 113   # Will return 113 to shell.
               # To verify this, type "echo $?" after script terminates.
               
    #  By convention, an 'exit 0' indicates success,
    #+ while a non-zero exit value means an error or anomalous condition.
    #  See the "Exit Codes With Special Meanings" appendix.

    Adapted from:
    "Chapter 6. Exit and Exit Status" by Multiple Contributors, The Linux Documentation Project is licensed under CC BY-SA 3.0


    13-C.6: Writing and Running Linux Scripts is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?