Passa al contenuto principale

1. Create and run a script

  1. Let's create a script that prints "Hello World!".

    vim hello.sh

    Press i then type this code:

    echo "Hello World!"
    echo "This is the first script."

    Press ESC then type :wq to save and quit.

    ls -l hello.sh
    cat hello.sh
    cat hello.sh | bash
    bash hello.sh

    We are sending it to bash, and bash is interpreting and executing the commands inside the script.

  2. We can tell the shell to use Bash for interpreting this script by adding #!/bin/bash as the first line of the script.

    vim hello.sh

    Press 1G, O, and then type #!/bin/bash.

    Press ESC, :wq and Enter, to save and quit.

    cat hello.sh

    The script now should look like this:

    #!/bin/bash
    echo "Hello World!"
    echo "This is the first script."
    The shebang

    If it was a Python script, we would have used instead the line #!/usr/bin/python3 to tell the shell that it should use Python for interpreting this script.

    The character # is usually called hash, and ! is usually called bang. Together they are called shebang and they are placed at the very beginning of a script (no empty lines and no empty spaces before them). After the shebang comes the program that the shell should use to interpret the script.

  3. Let's try to execute it:

    hello.sh

    It says command not found. This is because the shell looks for this command in certain directories, which are listed on the environment variable PATH:

    echo $PATH

    There is no command hello.sh in any of these directories, so shell cannot find such a command.

    To fix this problem, we can tell bash the path of the command, like this:

    ./hello.sh

    When we give a path to the command (./ in this case), the shell does not use the variable PATH but tries to find the command in the given path.

    Modifying PATH

    Another way for fixing the problem is to add the current directory to the PATH, like this:

    echo $PATH
    PATH="$(pwd):$PATH"
    echo $PATH

    Then the shell will be able to find hello.sh even if we don't specify its path:

    hello.sh
  4. Now, when we try to execute the script, it gives the error message Permission denied, because the script is not executable. Let's fix this by giving it the x permission, and try again:

    ls -l hello.sh
    chmod +x hello.sh
    ls -l hello.sh

    ./hello.sh
  5. In bash, comments are denoted by a #. Everything after a # is considered a comment and is ignored:

    ls -l hello.sh   # this is a comment and will be ignored

    # This is also a comment.

    Let's modify the script by adding some comments, and execute it again, to verify that the comments are just ignored by the interpreter.

    vim hello.sh

    Type 1G, o and then enter # This is a comment. on the second line.

    Press ESC, j, A and append # this is another comment on the third line.

    Press ESC, then :wq and Enter, to save and quit.

    cat hello.sh

    The script now should look like this:

    #!/bin/bash
    # This is a comment.
    echo "Hello World!" # this is another comment
    echo "This is the first script."

    Let's execute it and make sure that the comments are just ignored:

    ./hello.sh
Loading asciinema cast...