OS

What’s “#!/bin/sh” in scripts

had ever Imagined whats #!/bin/sh in your Shell Script??
You may have seen this many times in lot of scripts.
But question is that does it have any role in script?

YES.
image

In Linux/Unix System we combine many Terminal commands in single file,called as shell Script. It runs all commands Step by Step up to EOF(End of File).Whenever we want to run this script we use command as “sh /scriptpath/script.sh”

So now each time you want to run script we will issue above command .To avoid this we just add a line at Start of  of file which describes path to the interpreter in this case /bin/sh and is written in manner(Its called as Shebang line):
#!/bin/sh

note: don’t confuse between sh,bash,ksh,ash…..
you can google difference between them and decide which one to use…..

Not only shell script but other script languages like bash scripts,python scripts supports this shebang lines.
all you have to do is :
image

image

1)Write a script

2) add a line at top of file :
#!/path/to/interpreter

3)Make your script executable:
sudo chmod +x /path/to/script

4)Now you can directly run script by only issuing script name.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s