LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-11-2012, 01:49 PM   #1
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 51

Rep: Reputation: 1
different way of executing shell script


what is the difference between including poundbang in the shell script or not using poundbang in the script?

count.ksh
#!/bin/bash
echo "hello"

> ./count.ksh

other way

count.sh

echo "hello"


>sh count.sh
 
Old 07-11-2012, 02:14 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Ok, first lets clarify that its not called a poundbang but a "shebang".

This declares the location of the interpreter for the language. Yes a simple echo can be run without one as if there is not one it will assume that /bin/sh should be used which is a sym link to whatever shell you are currently using(ksh, bash, etc..).


The importance of this becomes clear as you try to create more indepth scripts in other languages. Here's how to see this in action:

Put the following into a file and make it executable.
Code:
print "I will now count my chickens:"

print "Hens", 25 + 30 / 6
print "Roosters", 100 - 25 * 3 % 4

print "Now I will count the eggs:"

print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6

print "Is it true that 3 + 2 < 5 - 7?"

print 3 + 2 < 5 - 7

print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7

print "Oh, that's why it's False."

print "How about some more."

print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2
Now try and run the script, it will error as bash or ksh cannot interpret these commands. Now add a shebang to specify python as your interpeter:

Code:
#!/usr/bin/python
And run the script and it will work.



NOTE: The above code was an excerpt take from http://learnpythonthehardway.org/book/ex3.html for use as a quick educational example.
 
Old 07-11-2012, 02:15 PM   #3
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
http://wiki.linuxquestions.org/wiki/Shebang
 
Old 07-11-2012, 06:11 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,389

Rep: Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774
Indeed your 1st example calls it count.ksh, but it will run using bash because bash was specified; not a good idea...
If no shell is specified ie no shebang line, it'll use the current default shell, which may not be what you wanted.
There are important differences between the various shell parsers.
For prod code ALWAYS specify the one you want as the first line.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
executing perl script inside shell script scriptme101 Programming 3 04-11-2012 09:30 AM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
Problem in executing a shell script ursvmg Linux - Newbie 1 05-19-2009 09:35 AM
Executing a shell script command!! vishamr2000 Programming 4 08-15-2005 12:33 AM
executing a shell script from C llama_meme Programming 2 04-01-2002 09:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration