How does calling it .sh help?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How does calling it .sh help?
# 1  
Old 05-17-2013
How does calling it .sh help?

Hi. I have been running some scripts ok with no extension on the name, and they work fine. What difference does it make if I call them whatever.sh? And I have some scripts starting #!/bin/bash - which debian recognises as shell scritps, even without the .sh ending - and some which don't. I'm sure this is all explained in a handy page somewhere but I can't find it. Please could someone point me in the right direction. Thanks.
# 2  
Old 05-17-2013
The ending doesn't matter to the OS. It is just for humans to get a quick idea, what this file is about.
The shebang (#!/.....) tells the shell which interpreter to use for the script. This is rather important while the ending is just a help.
# 3  
Old 05-17-2013
Naming your shellscripts with extension .sh (or .bash or .ksh,...) simply helps you to know that the file is actually a script without looking at its content.
Self documenting filenames make maintenance of a system easier.
# 4  
Old 05-17-2013
Windows operating system which mostly require file name extensions for applications/utilities to function properly.

Linux and Unix-like operating systems does not require file name extensions and it doesn't make any difference.

As cero mentioned it is used for easily identifying the file type.

You can run the file command to determine file type.

There are three sets of tests, performed in this order: filesystem tests, magic number tests, and language tests to classify the file.

Check the man page for further reference:
Code:
man file

# 5  
Old 05-17-2013
Thanks for all the answers. So I get that the extension is purely for human use. What difference does the shebang make? My scripts have been running ok without it. Does that mean that the system guessed right which interpreter to use? Is it good practice to always put it in? I am calling functions such as mkdir and rsync - does it make a difference if I put in the shebang or not? (I have read man file but I am not a lot wiser on this point as a result.)
# 6  
Old 05-17-2013
Quote:
Originally Posted by triplemaya
What difference does the shebang make? My scripts have been running ok without it. Does that mean that the system guessed right which interpreter to use?
The system doesn't guess at all -- it assumes /bin/sh if you don't tell it.

Which works great if your script is a garden-variety bourne shell. For Perl, it's wrong.

Quote:
Is it good practice to always put it in?
Yes, especially if you're using shell features like arrays which may not always be present in the garden-variety /bin/sh... /bin/sh may be a full-featured BASH on your system, but on others it may be a mouldy old shell with no features but pure Bourne.

So use #!/bin/sh for pure bourne scripts, #!/bin/ksh for ksh scripts, #!/bin/bash for bash scripts, and so forth. Use the exact right shell and people will know which it needs. Better they get a 'no such file' error than your script misbehaves and does unintended things.

Quote:
I am calling functions such as mkdir and rsync - does it make a difference if I put in the shebang or not? (I have read man file but I am not a lot wiser on this point as a result.)
These are not functions, they are external programs. As such they don't care at all which shell you use, any shell has access to them and they all run outside it.

Shell builtins on the other hand can vary between shells. /bin/sh may not have pushd/popd, but bash will.
# 7  
Old 05-17-2013
Thank you. This is getting much clearer. So is it good practice to put in /bin/sh for the kind of script I am running, or just leave it open as I have been doing? Is the bourne shell the 'vanilla' shell that ought to be called when calling other programs as I am doing?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling two function

Hi, I need to run start_load function for two tables. Step 1: if HMAX_TBL_ID and GMAX_TBLI_D are same for tab_name1 then echo message "all table ids are processed" Step 2: go back and call start_load for tab_name2 and check if table id are same for table 2 too. Please let me know how to... (5 Replies)
Discussion started by: sandy162
5 Replies

2. Programming

Calling template at once

Hello Again, I am just wanted to know if we can call the Template using "require_once" at PHP? Any views around happy to discuss. Thanks in Advance (2 Replies)
Discussion started by: AimyThomas
2 Replies

3. Programming

calling java from c++

I have an adapter written in c++ from which I need to call a java service point. Could anybody please provide an example program of calling java from c++ thanks in advance. (0 Replies)
Discussion started by: axes
0 Replies

4. UNIX for Dummies Questions & Answers

help with calling programs

Hi. I have a problem in running a program in linux system. This program (damaver.l86) is in the path /home/shenk/damaver/, and it needs to call another program (supcomb.l86) in another path /home/shenk/supcomb/. I tried to modified the .bash_profile, but it didn't work. The error message is always... (1 Reply)
Discussion started by: shenk
1 Replies

5. UNIX for Dummies Questions & Answers

Calling alias

I have created a shortcut for the file kill_process.sh as shown below. alias kill_process="sh /root/kill_process.sh" It is working as per my expectations. But I have 2 questions. 1) Is there any better way? 2) How do I check what does kill_process link to? (1 Reply)
Discussion started by: shantanuo
1 Replies

6. Shell Programming and Scripting

calling script

Hi all, The scenario is: 1. I created a script called hello 2. i created another script called blah which meant to use "hello" script. How do i do it? e.g. "blah" script #!/bin/sh test = /home/blah/hello echo "please enter name" read name test $name is it correct ? or i... (2 Replies)
Discussion started by: c00kie88
2 Replies

7. UNIX for Dummies Questions & Answers

Calling a function

I have created a file generic.func and it has lots of functions. One of the functions is this: Check_backup_size() { dsmc q b $BACKUP_DIR/"*.Z" | awk '{print $1}'|sed 's///g' > outputfile X=`awk '{sum += $1} END {print sum }' outputfile'` echo "$X" ls -ltr $BACKUP_DIR/"*.Z" | awk... (5 Replies)
Discussion started by: ashika
5 Replies

8. Shell Programming and Scripting

Get Calling File

OK, I'm very new to shell scripting, and I'm trying to write a (very) simple wrapper for sendmail that outputs all the arguments as well as the file that called sendmail to an output file which can later be looked over. Is it possible to get a file's name and path that called a script? (0 Replies)
Discussion started by: ghstber
0 Replies

9. Cybersecurity

i am trying to find out who is calling me

they are having an operator call my home line and also my cell number and they are typing and the operator tells me what they are typing on their computer. i nevere heard of this. it is new to me. it is free the operator said, they knew my name and also a friend of mine's name...the phone says the... (2 Replies)
Discussion started by: gail
2 Replies

10. Shell Programming and Scripting

Calling a script from another

Hi, I have 2 scripts, one is the .profile and the other is a SID selector for Oracle Databases. But when I call one from the other, even though it seems to have worked, it doesn't. Can anyone help?? ******************* .profile********************** #!/bin/ksh .... (5 Replies)
Discussion started by: dnkansah
5 Replies
Login or Register to Ask a Question