Writing Shell Script in Linux/Unix

Today I have tired with shell script which are meaningful and it is something I will remember as well.  Lets first discuss about writing sample shell script!

ubuntu linux icon

1. Decide about a place to place your shell scripts. Usually it can be /home/yourUserDir/bin (/home/satya/bin)
So, create a directory if it does not exist.

Go to your home (~) dir and run
$mkdir bin

2. create a file using a text editor. I used vi to create a shell script. First cd into bin/ directory.
$vi testsh

3. Write any command you run on shell prompt.
echo $PATH

4. Close the file and give it a executable permission.
$chmod 755 testsh
Or,
$chmod ugo+x testsh

5. Use the script you just created.
$./testsh
It should output your path info.

6. Now, it may be running as you are inside bin/ directory.
You can include the bin/ folder in the PATH of executable. Open your .bashrc (dot bashrc) file and append your path (/home/yourUserId/bin) to the path.
$vi ~/.bashrc
Usually in this format:
export PATH=${PATH}:/usr/local/bin/:/home/satya/bin

Run it on your command line to activate new changes you made:
$source .bashrc

7. Check your path ($PATH) is updated or not!
$echo $PATH
If you see your newly added path along with old path then it is all correct.
Try running your script from anywhere. It will execute and give you output.
$testsh

This was all you need to write shell script in .

How to provide --help functionality to your shell script? Follow the post mentioned at the bottom, to know it.

For more here is easy to read and understand article about Linux shell script.
More about file permission.

Another post on shell scripting - Printing Full Path with Filename in Linux