Skip to main content
Chemistry LibreTexts

4: Bash Shell Scripting

  • Page ID
  • \( \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. 


    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 >
    cat >


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


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

    echo "this is a test sentence"



    Running the file with Bash using the command:

    • bash



     Checking the permissions of the file:

    • ls -l
    ls -l


    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
    chmod +x
    ls -l



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

    • ./


    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?