3

If I want a series of commands to be run by terminal, instead of writing it in a text file and copying and pasting it in there, could I have terminal run it? If so, what does the extension have to be?

3

What you are trying to do is create a shell script with a .sh extension (Windows equivalent is a batch file with a .bat extension). Here is a simple tutorial to get you started, here is a more complex one that contains a "hello world" style introduction to shell scripting.

6
  • 1
    @typoking It won't let me use .sh. I can either use .txt or both. How do I isolate .sh?
    – JShoe
    Apr 7 '11 at 2:31
  • Why can't you use .sh? You said you can use ".txt or both" but .txt is only one thing... what does "both" signify? Have you already read through and tried the link to the tutorial I posted a link to? Apr 7 '11 at 2:35
  • Ok sorry, I fixed that by getting BBEditor. I did the chmod +x filename.sh thing, but the file didn't change. I used the actual name btw. It didn't become executable.
    – JShoe
    Apr 7 '11 at 2:37
  • Sorry again, I figured it out on my own. Thanks!
    – JShoe
    Apr 7 '11 at 2:39
  • @JShoe don't be afraid to mess around try out some tutorials, you will learn a lot through the pain and suffering of figuring these things out. Apr 7 '11 at 2:43
6

The accepted answer is misleading. On OS X (and U*x generally), the name of the file doesn't matter at all. What matters is that it needs to have executable permission (chmod +x file) and a correct shebang line.

bash$ cat > randomness
#!/bin/sh
echo Hello, world
Ctrl+D

bash$ chmod +x randomness

bash$ ./randomness
Hello, world

bash$

The first line looks like a comment, but it's a comment in a special format, called a shebang (short for sharp-bang, common names for the characters # and !). The two characters need to be immediately followed by the absolute path name to the interpreter which this script should be interpreted by (optionally followed by whitespace and options for the interpreter; for portability reasons, only a single option string is allowed).

1
-3

Just write/paste your commands in TextEdit app then save it to desktop .rtf, and once its on the desktop press on the title and rename to your preferred extension. A window will pop up asking you if you are sure you want to save as... select your option. Done. The icon should change to represent the app that can open the extension. Good luck.

2
  • He doesn't have a prefered extension, he was asking how to essentially create a batch file. Mar 4 '15 at 2:43
  • Downvote: Saving as RTF definitely does not work.
    – tripleee
    Jan 6 '17 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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