BradTrupp.com - Little slices of my life and my projects BradTrupp.com -- Tags -- Code
BradTrupp.com -- Tags -- Code -- Bash - An Introduction to Scripting (Bash)

Bash - An Introduction to Scripting (Bash)
(2007/10/10)

Bash is a Unix shell program and is the default shell for Ubuntu, as well as many Linux systems and Mac OS X.

A Unix shell, which is commonly called "the command line", provides the traditional user interface for the operating system. You use it by entering commands for the shell to execute, but can also write and execute complex shell programs to do most anything.

In Microsoft Windows, this would be equivalent to command.com (or cmd.exe) for the command prompt.

A Basic Bash Script

It is best to start your Bash script with the "shebang" line -- although it is somewhat optional being the default shell anyway.

#!/bin/bash

Note: The "shebang" (also called a hashbang, hashpling, or pound bang) refers to a pair of characters "#!" that, when used as the first two characters on the first line of a script, causes Unix-like operating systems to execute that script using the interpreter specified by the rest of that line.

It is also good practice to end your script with the exit command.

exit 0

Here is the standard over-used "Hello World" example you see in so many programming languages.

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

Enter these 3 lines into a file using the text editor of your choice and save it as "helloworld".

Bash Comments

Comment lines in Bash scripts are similar to other programming languages where a special character, in this case "#" (the hash or pound sign), is used to delimit the start of a comment line.

# This is a comment line

Make it Executable

Until you mark your script as executable, it is nothing more than a text file.

The easiest way is to use the chmod command to change the file permissions.

Open a terminal session, navigate to the directory that you saved "helloworld" into and enter the command:

chmod u+x ./helloworld

Running a Bash Script

Once again, open a terminal session, navigate to the directory that you saved "helloworld".

If you enter the command

helloworld

you will likely get an error like "command not found".

This is because the default search path for commands does not include the current directory -- so the shell can not find your program.

Now enter the command

./helloworld

and your script should run and display the expected "Hello World!" output.

The "./" means look in the current directory.

You can also fully qualify the script (assuming the script is in freddie's desktop)

/home/freddie/Desktop/helloworld

and again the script should run.

What's Next?

Now we are ready to write some useful scripts, but first here are some related web sites.

Tags: Code

Share: Del.icio.us | Digg | Facebook | Google Bookmarks | Reddit | Technorati | Twitter | Windows Live | Yahoo! My Web

View Comments (0)


 

Tag: Code
A Little Bit of Automation for your Backups (2010/02/25)
A Simple CSV to iCalendar Conversion Utility written in Ruby (2009/06/29)
A Simple iCalendar to Task Coach Conversion Utility written in Ruby (2009/05/09)
Using Ruby to generate Social Bookmarks for your Web Pages (2008/11/23)
Using Ruby to fix id3 tags on mp3 files (Ruby) (2008/11/10)
Copy all files in a directory tree to a common destination. (Ruby) (2008/10/08)
Check for File Changes using Ruby and MD5 (Ruby) (2008/08/20)
Create Checkpoints using Ruby and MD5 (Ruby) (2008/07/30)
Bash - A Basic Greeting Program. (Bash) (2007/10/31)
Bash - An Introduction to Scripting (Bash) (2007/10/10)
UUMerge Command Line Text File Merge (Delphi) (2007/07/01)
Explorer-Style Fly-By Buttons (Delphi) (2006/01/14)
Handle-Free Checkboxes in a String Grid (Delphi) (2006/01/14)

All Tags
Business Tips (5)
Code (13)
Life Skills (1)
Music (2)
My 15 minutes (5)
Old Articles (39)
Photos (10)

Advertisement