Skip to main content

Bash: Our First Script

So you want to create your very first bash script? Well it's actually pretty easy, just open your favorite text editor (vi, emacs, gedit, kate) and type:

#!/bin/bash
echo "Hello World!"

Save this file under your /home directory (i.e /home/yourname), with the filename of firstscript.sh. You will then have to give the script execute permissions for yourself. In order to do this you will need to use the 'chmod' command:

[user@localhost ~]$ chmod 700 firstscript.sh

Now your script can now be executed:

[user@localhost ~]$ /home/yourname/firstscript.sh
Hello World!

Start Off With A She-Bang!

The She-Bang is the first line in this miniscript:

#!/bin/bash

This tells the shell to run this script using bash. Why do this? Well maybe you want to create a PHP script or a script for the 'awk' command. Without the she-bang, the shell won't know how to interpret the rest of the file.

Echoing

The next line uses the 'echo command to "echo" text to the screen. Echo can be seen as a way to send text to the screen to keep the script user informed of what is going on in the shell. Using properly placed 'echo' statements can give the user valueable information as to what the script is currently doing as well as help the script's programmer in debugging the script itself.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <p><b><i><pre><a><img><em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><h1><h2><h3><h4><h5><h6>
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options

Premium Drupal Themes by Adaptivethemes