User Tools

Site Tools


writing_a_shell_script

This is an old revision of the document!


Writing a Shell Script



To successfully write a shell script, you have to do three things:


  1.Write a script
  2.Give the shell permission to execute it
  3.Put it somewhere the shell can find it


Writing A Script


A shell script is a file that contains ASCII text. To create a shell script, just use a text editor. Here is an example of a simple Shell Script:

  #!/bin/bash
  # My first script
  
  echo "Hello World!"
  
  Now save the file as helloworld


The first line of the script is important. This is a special clue, called a shebang, given to the shell indicating what program is used to interpret the script. In this case, it is /bin/bash. Other scripting languages such as Perl, awk, tcl, Tk, and python also use this mechanism.

The second line is a comment. Everything that appears after a “#” symbol is ignored by bash. As your scripts become bigger and more complicated, comments become vital. They are used by programmers to explain what is going on so that others can figure it out. The last line is the echo command. This command simply prints its arguments on the display.

Setting Permissions


The next thing we have to do is give the shell permission to execute your script. This is done with the chmod command as follows:

  chmod 755 helloworld


The “755” will give you read, write, and execute permission. Everybody else will get only read and execute permission. If you want your script to be private (i.e., only you can read and execute), use “700” instead.

writing_a_shell_script.1470239089.txt.gz · Last modified: 2023/03/09 22:35 (external edit)