How to create a simple shell script

Here is a simple example of creating and running (executing) a shell script. This script will display the numbers from 1 to 10.

Creating the script:

  • With any text editor (Gedit for Gnome, Kate for KDE, nano):
    #/bin/bash
    for ((i = 1; i <= 10; i ++))
    do
        echo $i
    done
  • Save it as myscript.sh.

Setting the execution rights:

Open a console, go to the directory where the script was saved and type:

chmod +x myscript.sh

Running the script:

  • In the console type:
    ./script.sh

    or

    sh script.sh

    The result should be like this:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

About Cristian Radulescu

PHP // MySql // Linux // Android
This entry was posted in Linux, Programming and tagged , , . Bookmark the permalink.

One Response to How to create a simple shell script

  1. Pingback: Understanding file permissions and ownership on Linux | Cristian Radulescu

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>