How do I make a .sh file unix executable?

I have a .sh file that is showing type as a plain text file. I need to make it a Unix Executable file. When I try to do this in Terminal with a chmod command it doesn't change.

There's other odd behaviour. If I make a duplicate of the file ('xxxx.sh copy') it says it's a Unix executable file. If I delete the 'copy' bit, it's back to a plain text file. If I leave a space after sh, it stays executable. But I'm not convinced it is.

Can anyone help?

iMac 24-inch, Mac OS X (10.5.5)

Posted on Dec 9, 2008 4:29 AM

Reply
9 replies

Dec 9, 2008 9:14 AM in response to nigmia

nigmia wrote:
Mailman, I get

-rwxr-xr-x

when I do what you suggest. I think the xs mean it is executable, don't they?


You are aware that unless the script is in the path, you need to precede it with a ./
if the script is basharooni.sh
to run it if it is not in the path you need to enter
./basharooni.sh

It's also good, if you have not already done it, to include a shebang in the file as the first line thus:
#!/bin/bash
code here

not necessary, but good scripting practice.

Dec 9, 2008 10:04 AM in response to nigmia

Are you trying to create a double-clickable file?

Or do you just want to execute it from a Terminal command line?

From what you have been saying, it sounds like the .sh has an *Open With...* association to a program such as TextEdit, or similar text editor.

As others have said, from the Terminal command line prompt you can run the script by typing something like ./yourscriptname.sh

But if you want to double-click on the file from the Finder, then you would do better to remove the .sh type field altogether, or giving it a type of .command

Then again, double-clicking on shell scripts does not always yield the results expected 🙂

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How do I make a .sh file unix executable?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.