Computing Magazine

Executing Our First Script

Posted on the 15 October 2011 by Skiabox @skiabox

Command Line

Today we’ll see how to run our first program from the command line.

First we should be in our home directory. To do that type :

> echo $HOME

In my case, executing this command has the following result:

/Users/myUserName

 

Now we must select a directory for the script we want to execute.

When the shell we use is given a command to execute, it looks for the command name in the directories listed in the PATH variable.

This variable contains a colon-separated list of directories that contain executable commands.

A typical value for $PATH is :

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin    (You can find the value of your $PATH variable using ‘echo $PATH’ at the command prompt)

 

Commands are usually stored in directories named bin, and a user’s personal programs are stored in a bin subdirectory in the $HOME directory.

So let’s create bin directory after making sure that we are in our home directory (using pwd command we can quickly see the full path of our current directory).

> mkdir bin

 

Now that it exists, it must be added to the PATH variable:

> PATH=$PATH:$HOME/bin

 

The next step is to create the file

> echo echo Hello, World! > bin/hw   (The greater-than sign (>) tells the shell to send the output of a command to the specified file, rather than to our screen)

 

Next we should make this file executable. To do that, we give the file execute permissions:

> chmod +x bin/hw

 

We have finished now and the command can be run using just its name:

> hw

Hello, World!

 

In my next article I’ll talk you more about file permissions and how to use chmod command to add or edit them.


Back to Featured Articles on Logo Paperblog