Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

PART 1 Ok so i'm using Mac and I started out with a file called...

Example: myFile.sh

In Terminal I ran this file in its directory by typing...

bash myFile.sh

That works perfectly, but then I did some research about bash vs sh and found that I could have also written it like this...

sh myFile.sh

That also worked. I did some more research and an idea came to mind, that since bash is the "newer" way to do things, I thought I would try to rename myFile.sh to myFile.bash to see if it would still run in terminal if I typed this...

bash myFile.bash

And it worked perfectly! But now I'm wondering if there is a reason that the file was originally named myFile.sh instead of myFile.bash

So, is it bad that I renamed it to myFile.bash instead of myFile.sh? Or is it totally fine??

(i'm new to Terminal commands)

PART 2

Another question: How would I make this "myFile.sh" executable? This is the contents of myFile.sh below:

#!/bin/bash 
python runtime/recompile.py "$@"

NOTE: I did some research and tried renaming myFile.sh to myFile.command which should make it executable when I open the file, but it didn't work and I got this error in Terminal:

python: can't open file 'runtime/recompile.py': [Errno 2] No such file or directory

So my questions really are:

a) How do I make myFile.sh executable in Terminal? b) Why didn't renaming myFile.sh to myFile.command?

Any help appreciated.

share|improve this question
1  
The can't open file message means simply that runtime/recompile.py doesn't exist. That has nothing to do with the name of your myFile.whatever script. But you probably don't need the shell script at all; just make sure your Python script has #!/usr/bin/python, run chmod +x on it, and run it directly. –  Keith Thompson Dec 6 '11 at 9:46

3 Answers 3

up vote 3 down vote accepted

It is fine, it will work with different extensions and without extension as well. File extensions are meaningless in Unix (reference: http://developers.sun.com/solaris/articles/korn_shell.html). Extensions are useful for humans to identify files quickly.

You should probably use .sh extension as it is more popular and shorter than .bash.

share|improve this answer

You should probably make those files executable using chmod +x filename and specify the script interpreter in the first line (shebang):

#!/bin/bash

Or, to respect $PATH:

#!/usr/bin/env bash

This way, the file itself specifies what script interpreter to use. You can even specify arguments to bash this way. If you require the configuration applied to login shells inyour script, you could do the following:

#!/bin/bash --login

Using a custom file extension such as .bash might break non-obvious processes, such as FTP file transfers if you are also using Windows systems: Many FTP programs treat files with specific extensions as text, changing the line endings. If you use non-standard file extensions for your scripts, this won't happen and \r (carriage return) characters from Windows will be retained.


Your renaming to .command probably does not work, since your working directory changed along with how you execute your file. It will always be executed in $HOME, e.g. /Users/danielbeck, and relative paths need to start there. It's safer to provide an absolute path.


If you don't require a Terminal window for program execution, you can also use Automator and its Run Shell Script action to create an application or service, the latter with optional keyboard shortcut in System Preferences » Keyboard » Keyboard Shortcuts » Services.

You need to configure the service to receive files and folders in any application as input, and execute it when having files selected; if it's an application, you can drag&drop files onto it. In any case, configure the Run Shell Script action to receive input as arguments.

share|improve this answer
    
Yeah, making it executable will be alot quicker than typing. Now, if I make it executable, does that mean I can just double click the file and it will work? Here is the code inside "myFile.sh": #!/bin/bash python runtime/recompile.py "$@" (its for minecraft modding) in Terminal, what would I type to make it executable? NOTE: I tried renaming the extension from .sh to .command (it didnt execute when double clicked, there was an error), also would it be better to make it into a .app some how?? - or would that be pointless since making it executable would do the same thing* –  Jacob Dec 5 '11 at 17:09
    
@Jake Please edit your question, or post a new one, so you actually ask for what you want to accomplish. It appears this is a classic XY problem. You probably need neither a bash script nor an executable file that launches python. Let me know once you've done it by commenting here, so I can post an answer. –  Daniel Beck Dec 5 '11 at 18:25
    
Ok, I made a Part 2 to my question above. –  Jacob Dec 6 '11 at 2:42
    
@Jake "Multi-part" questions are generally frowned upon, as you're prone to sending us on a goose chase, and it's difficult to determine what user actually answered your question. The usefulness to others looking for an answer to a similar question is also very limited. –  Daniel Beck Dec 6 '11 at 4:05

The first line of an executable shell script should be a "shebang"; the #! sequence is a "magic number" that tells the system that the rest of the line specifies an interpreter to which it will pass the name of your script.

For example, if you have a file named "foo" (no suffix needed) that looks like this:

#!/bin/sh

echo This is foo

and you've run chmod +x foo (that's important!), then this:

./foo

is equivalent to this:

/bin/sh ./foo

and will produce the output This is foo.

You can call the script foo.sh, for example, if you want to emphasize that it's a shell script, but it's not necessary. (Typically there's no reason for the user to care how it's implemented; in that case, you can just call it foo, which could be a shell script, a Perl or Python script, or a compiled executable.)

So what's the difference between #!/bin/sh and #!/bin/bash? For most purposes, they're interchangeable. /bin/sh is generally the Bourne Shell, which dates back to the early days of Unix; you'll find versions of it on all Unix-like systems, including Linux and MacOS. /bin/bash is the GNU "Bourne-Again Shell", which includes the standard Bourne shell functionality plus a number of extensions.

If you use only features that are supported by the Bourne shell, it's probably better to use #!/bin/sh just as a matter of style, and to make it more likely that your script will run without change on other systems (which might not have Bash installed).

If you use Bash-specific features, you'll need to use #!/bin/bash.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.