Skip to main content
Chemistry LibreTexts

4: Bash Shell Scripting

  • Page ID
    414179
  • \( \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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

     

    What is Shell Scripting?

    Shell scripting is a part of the automation process in Linux. Scripting allows writing a sequence of commands in a file and executing them. 

    It helps in saving time because the user won't have to repeat writing commands. They can schedule tasks for automatic execution. They can also make scripts that are executable on startup.

    What is Bash Shell?

    It is the default shell for many Linux distributions. Its script is generally located in the .bashrc file, which allows the customization of the shell.

    What is a Bash Script?

    A Bash script is a file containing a collection of commands that are read and executed by the Bash program. 

    Characteristics:

    A Bash script is characterized by:

    - its file extension .sh

    - needing the execution rights (x) added to the file in order to be able to execute via Bash shell

    - being executable through an absolute path, through a "Shebang"

    Shebang is composed of "bash = #" and "bang = !" and the path of the Bash shell. 

    It is added to the first line of the script and its function is to make the shell execute the script via Bash shell.

    One example of a Shebang is #! user/bin/bash

     

    Operations on Bash script

    Setting the Bash interpreter and making it executable

    Let's create a Bash script by making a new file and adding some content to it. 

    Creating the file using the command:

    • cat > test.sh
    cat > test.sh
    cat test.sh 

     

    Adding a command to the content of the file using the nano editor:

    nano test.sh 

    and then in the script run an echo function to print a sentence to the screen once the bash script is run

    nano test.sh
    echo "this is a test sentence"
    
    Output

    clipboard_e39241a775d549197b16d72d36854e4e9.png

     

    Running the file with Bash using the command:

    • bash test.sh
    bash test.sh
    
    Answer

    clipboard_ee08771559af174f852256b4eeac77f15.png

     

     Checking the permissions of the file:

    • ls -l test.sh
    ls -l test.sh
    
    Output

    clipboard_ebcc6cd8a2ceaee36b482aff1d7480c26.png

    Notice that the file doesn't have execution permissions. Thus it cannot be executed via Bash.

     

    Thus, let's make it executable and then check that the permissions changed:

    • chmod +x test.sh
    chmod +x test.sh
    ls -l test.sh
    
    Output

    clipboard_e60ce058750b6255eff580bb50a1db290.png

     

    Now, we are able to run our script with the default interpreter without having to use the "bash" command, as follows:

    • ./test.sh
    ./test.sh
    
    Output

    clipboard_e4fa85c4175d976fee2557914946f6642.png


    This page titled 4: Bash Shell Scripting is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?