Bash: How to make an executable script

I want little by little to put articles on BashWell, I have enough material to teach you little by little tips, make scripts, and much more, so that our daily tasks are automatic, so obviously it will save us a lot of time 😀

Now I will show you the basics, what you will always need to know, and that will help me for the rest of the tutorials 😉

How to make a .sh script?

Simple ... very simple 😀

1. Open a terminal, in it write the following and press [enter]:

cd $HOME && touch script.sh && chmod +x script.sh

This will be enough to create a file for them script.sh at their personal folder.

2. In the terminal put the following:

cd $HOME && echo '#!/bin/bash' > script.sh && echo '# -*- ENCODING: UTF-8 -*-' >> script.sh

3. Ready, you have your script ready 😀

If we open it, we will have something like this:
#!/bin/bash
# -*- ENCODING: UTF-8 -*-

After that second line, the instructions are written from there.

For example, we will tell you to show us in a terminal «<° Linux is the best»😀

We would have the script as follows:
#!/bin/bash
# -*- ENCODING: UTF-8 -*-
echo "<° Linux es lo mejor"
exit

How to run or test a .sh script?

1. We must go to the folder where the script is, in the previous example it would be our personal folder, so we open a terminal, in it we write the following and press [enter]:

cd $HOME

2. Now we execute it by putting a point and a slash (followed), and followed by the name of the script, that is:

./script.sh

And bingo, we already have it 😀

Do it and you will see ...

Now an important detail, at the end they should always put «exit«

And well now, nothing more to add, just wait for future tutorials, here you will learn bash HAHA.

regards


57 comments, leave yours

Leave a Comment

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

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Oscar said

    Thanks friend, I was always curious about how to make a script.sh, now it's time to learn, I'll be waiting for the next tutorials.

    1.    KZKG ^ Gaara <"Linux said

      Nah, a pleasure hehe 😀
      You'll see ... little by little I'll be putting Bash tutorials, to see if someone gets excited, learns and we all get better HAHA.

      regards

      1.    jose said

        Hi, I would need your help if you can help me with some scripts that I have stuck and I need to do for a subject, I would love to get in touch with me.
        Thank you very much in advance

  2.   brown said

    Ah mortal !! great 😉

    1.    KZKG ^ Gaara <"Linux said

      Thank you
      Remember that complaints or suggestions will always be well received 😉

  3.   taregon said

    I liked the post, whenever I need a script header I embark on navigating only to find the line: "#! / Bin / bash" in the header (I'm very forgetful). Now with this contribution I can have it written down and just copy and paste 😀

    1.    KZKG ^ Gaara <"Linux said

      Remember that it is / bash and / sh ... it's different haha, I once spent 2 days fighting with a script that didn't work for me as it should, and it was because I put sh instead of bash 😀

      Any questions you tell us.
      regards

  4.   xfraniux said

    Jaajajajajaja and this is the simplest, you could also open gedit or any editor and copy:

    #!/bin/bash
    # -*- ENCODING: UTF-8 -*-
    echo “<° Linux es lo mejor”
    exit

    And then we give it execution permissions….

    very good data .. greetings

    1.    KZKG ^ Gaara <"Linux said

      hehe yeah, that could have been done like this, but I don't know ... I thought it would be simpler to do a copy / paste of two lines (which can actually be one) and that's it, ready the script with execution permissions and the header 😀

    2.    bertholdous said

      Hello. Should scripts always be saved as a .sh file?

      In windows its similar would be the .bat files. And as for their writing, they look a little simpler.

  5.   Lucas Matthias said

    Very good che

    1.    KZKG ^ Gaara <"Linux said

      Thank you

  6.   arthur molina said

    I look forward to the next post and for the moment taking note.

    1.    KZKG ^ Gaara <"Linux said

      😉
      Any suggestions, script you want me to do or something? 😀

  7.   Courage said

    Is this the one who doesn't know how to program? Although it's easy, it's programming

    1.    KZKG ^ Gaara <"Linux said

      HAHAHA come on ... are you excited? ... learn a bit of Bash, you'll see how great it is, you don't need to know how to program, far from it 😀

      What do you say?

      1.    Courage said

        I will, today I'm not for that

  8.   Anonymous said

    In fact, if scripts are going to be created regularly, the task itself could also be automated with a script like the following (it is only copied to $ home / bin / and given execution permissions)


    #!/bin/sh
    # nuevoscript
    if [ $# -eq 0]; then
    DEST=$HOME
    SNAME=script.sh
    elif [ $# -eq 1]; then
    DEST=.
    SNAME="$1"
    else
    echo "Parámetros incorrectos"
    exit -1
    fi
    echo -e '#!/bin/bash\n# -*- ENCODING: UTF-8 -*-' > "$DEST/$SNAME" && \
    chmod +x "$DEST/$SNAME"
    echo "Creado el script $DEST/$SNAME"
    exit 0

    In this way, if you run new script without parameters, create $ home / script.sh, but if it runs new script other script, creates ./otherscript

  9.   Ramses said

    Hello friend, I would like to know how I can create an autorun for a sdcard and that it be read by my phone with so android and that when it is executed I start abd.exe plus the command bugreport> bugreport.txt

    1.    KZKG ^ Gaara said

      No idea friend ... I've never used Android.

  10.   Neo61 said

    Thanks Gaara, I wanted to find a place where I could be taught some scripts, I think that in an email I mentioned this to you which is really something I need to learn. I followed the steps you detail and everything is perfect but it does not run, I get this:

    ./script.sh: line 5: Unexpected EOF while searching for a matching `» '
    ./script.sh: line 9: syntactic error: the end of the file was not expected

    Explain to me what my blunder is

    1.    KZKG ^ Gaara said

      My fault, WordPress changes some details, look again at the code in the post and put it like this in your script.
      What happens is that:

      "Asd"

      It is not the same as:
      "asd"

  11.   Neo61 said

    I don't understand, I still see it the same. Where is the change? Could you explain me better? Now when you give it execute, this line comes out:
    ./script.sh: line 5: °: The file or directory does not exist

    1.    KZKG ^ Gaara said

      Pass me the script you are running, put the code here: http://paste.desdelinux.net
      Look at the post again, it is that I had forgotten to click the Update button 😀

  12.   Neo61 said

    Hi partner:
    I will tell you that I was looking at the conkyrc script that you made for your conky 2010 and if, it is true, the disk symbol belongs to a source called Poky but it does not come by default in libreOffice in Ubuntu 12.04, it would be good, if you have this source Tell me where I can download it from (Something a bit difficult, really), one thing that I have noticed is that when the letters of these symbols are put and the font does not exist in the system, then it puts the letter, not the symbol, It is logical, I am already getting an idea of ​​how everything is working, but I need someone to explain things to me to understand it better, everything I do is by deduction and I have never given any programming, it was not computer science what I studied And also, it was another time, I have computing as a hobby and the good thing is that I am working in one of its branches, everything I have learned has been self-taught, so my interest in someone who, even little by little, Guide me. here is also the script of my conky and I'll explain what I don't get:

    Look, when I try to insert another HDD I keep getting the same one because it gives the same temperature value. I do not get the CPU characteristics in the system, it comes out (NULL) in bold, when I try to insert the calendar that you put in the Conky that tanias in 2010 is disorganized and widens the conky bar. Look to see all the improvements you can make. I like it with the width that I have and that your calendar adapts to that width and that everything I told you that does not show comes out, here it goes:

    http://paste.desdelinux.net/4552

    –Put the code in our paste, so the comments are not so extensive–

  13.   GONZALO said

    the truth is that your contribution is regrettable

  14.   edgar said

    Hello, you have made my life easier with this script. Thank you very much for I would like to know if you can send me an email where you describe it since I am a nevo in programming.
    Thank you

    1.    KZKG ^ Gaara said

      Hello,
      Better I have not been able to describe / explain these lines of code, what have you not understood to explain in another way?

      Maybe if you have questions you can always ask in our forum: forum.desdelinux.net

      regards

  15.   Caesar Salad said

    Nice contribution but you could round it up ... I need a script for a sai. That is, when the Sai realizes that it is on battery and has 20 minutes to turn off, it sends a signal to a device and that has to execute the script to turn off some servers. I do not know if I have explained myself well ... Valdria with putting in your script «shutdown -h» ??

    Thank you!!

  16.   Jesus Israel Perales Martinez said

    Something else to add to my template folder: B

  17.   Edward said

    Could someone help me I need a script for ubuntu that if we close the chrome browser, open it again

    Thanks in advance

  18.   edo said

    I need a script that just by executing it will overwrite the information of a file with another text, does anyone know how it is?

    1.    edo said

      I already saw that above they say how to do it

  19.   Dario said

    Very good, excellent for my grandchildren.
    A thousand thanks. »Just as you would have many young people, acting as teachers… .it would be wonderful.

  20.   Roman PC said

    Simple and functional, as it should be.

    Thanks for sharing.

    Greetings.

  21.   Hernan Jaramillo said

    Thanks for your help. It was useful, very good explanation.

  22.   vicene said

    Thank you it served me. Cheers

  23.   Gamerz said

    Simple and effective. Great tutorial for newbies 🙂

  24.   Luis Carlos said

    Hello, I know very little about scripts and I do not know if I am confusing concepts but what I am trying to do is in the following web page:
    http://beginlinux.com/blog/2010/03/iptables-with-network-card-aliases/

    The point is that I don't really understand how to start this code or if this replaces the IPTABLES. And if it replaces it as it would be so that it starts automatically with the OS.

    Thank you

  25.   Cris said

    Terrific Gaara !!!

    I started doing the homework as you explained and it worked !! Thank you for taking the time and sharing your knowledge to emerge from the darkness with the ignorant.

    😉

  26.   leo said

    Hello, I have any questions to make a script that a user creates, could you help me?

  27.   young said

    hello I have a question with a .sh file
    do you have any way that
    I contacted you to see if I am doing it right?

    #! / system / bin / sh
    mount -o remount, rw /
    mkdir /mnt/local/Android/data/org.xbmc.xbmc/files/.xbmc
    ln -s /mnt/local/Android/data/org.xbmc.xbmc/files/.xbmc /.xbmc
    mount -o remount, ro /

    , I run it manually in the terminal and if it works, but when I want the file to run it doesn't want to.

  28.   louis said

    I found this one at vivaolinux.com.br, it's great

    #! / Bin / bash

    Based on Gera.sh - Gera cabeçalho for scripts

    Written by: Sandro Marcell P. Barbosa (Boa Vista - Roraima)

    Email: sandro_marcell@yahoo.com.br

    Slackware GNU / Linux 10.1.0

    Usage example: script_name my_script

    You can also specify the extension, conformity or interpreter.

    Ex .: nome_script backup.sh for o interpreter 'sh'

    or nome_script backup.tcl for the 'tcl' interpreter and also for the day!

    Defining the interpreter you can (substitute for another!):

    INTERPRETER = »#! / Bin / sh»

    Header content (change as you like!):

    INFO = »##»
    CREATOR = »## Written by:»
    EMAIL = »## E-mail: you@correo.com»
    DISTRO = »##»

    Checking if the user specified the script name:

    if [$ # -eq 0]; then
    echo ">>> Usage: $ (basename $ 0)"
    exit
    fi
    if [$ # -ge 2]; then
    echo "Name with spaces are not valid!"
    exit
    fi

    Can the user write to the current directory?

    if [! -w $ PWD]; then
    echo "No permission to write the current directory!"
    exit
    fi

    What if there is another script with the same name in the current directory?

    if [-f $ 1]; then
    echo "A script with the same name already exists in this directory!"
    exit
    fi

    Script body:

    (
    cat << END
    $ INTERPRETER

    $ INFO
    $ CREATOR
    $ EMAIL
    $ DISTRO

    Now add commands on subsequent lines =)

    Creation date of this script: $ (date «+% a% d /% m /% Y») at $ (date «+% T»)

    END
    )> $ 1

    Setting execute permission:

    if [-f $ 1]; then
    chmod + x $ 1 2> / dev / stdout
    echo "Script $ 1 created and allowed to execute!"
    fi

    Creation date of this script: 29/01/2013 19:45:00

    1.    debianistrowler said

      Great, it works perfectly !!!

  29.   Vicente said

    Very good tutorial, simple and it has helped me a lot, thanks

  30.   calichi said

    Dear KZKG I am a newbie, but I am interested in learning.
    You have other scrip. Or suddenly in the place where I work I want to open one that is compiled and I can't see it.
    Some idea.

  31.   calichi said

    I require help with a script. It is compiled.

  32.   cool9 said

    I understood about the bash, but if I want to make an automated alias it does not come out

  33.   angiesarite said

    Thank you very much for your help friend.

    I would like to know if you can do me a huge favor, I need to create a script that helps me create a .sum at a certain time but I don't know how to parameterize it. You may be able to help me with some pointers. Thank you and I'll be attentive if you can help me.

    regards

  34.   José said

    Hello, good afternoon, look, I am doing a job in lex, so I would like if you can help me with a Script, with which I can run the lex file, the one that generates lex (lex.yy.c) and the data input file .

    Thank you very much

  35.   wilmer ron said

    watercress thanks doc !!! I'm new to the script, thank you very much, greetings, I'll be on the lookout with your new tutoring !!!!

  36.   Karen vega said

    Hello!

    Thank you very much for your contributions, I am starting to get into unix and it is rare that someone explains this universe of code in such a simple way. I would like to ask you if with your procedure I can make a script that helps me find n number of files that live in the same path, and that I copy them to another folder ... someone told me that I can store in a txt the path and the name of my files, but it is not clear to me how to do it. I remain attentive.

    Regards!!

  37.   eibar amaya said

    Hello I have a VPS but I need help in some things orita myself I am interested in how to create a script or something so that I run a self backup of everything that is inside my / root folder and that backup runs every 1 hour if I could you help in that i would appreciate you too much in that

    I'll leave you my facebook if you want to help me I need a lot of help 🙂

  38.   Jorge Rodriguez said

    create me one to ping and monitor certain network equipment in the company
    but i made it simple

    touch red.sh && chmod + x red.sh
    threw out '# -- ENCODING: UTF-8 -- '>> red.sh
    echo 'ping 10.50.0.125 -w 5' >> red.sh
    echo 'ping 10.50.0.80 -w 5' >> red.sh

    run it ./red.sh and fine

  39.   Bill said

    Dear, I need to create an automatic installation script. I am willing to pay for a job well done. Those who are interested, send me an E-mail to carranzalh@gmail.com. Thanks

  40.   sary said

    they suck my cock XD

  41.   hdexz said

    Good morning friend, could you help me with something
    I need to make a backup with linux of the company since it is safer so that viruses do not catch it but I do not know how to do them
    I need to create a script which will pack the files and send them to an ftp

    I would appreciate in advance write to my email

    cesarloscor@gmail.com