Categories: Miscellaneous

How To Make and Run Batch Files In Terminal In Mac OSX

I use batch files sometimes when I was using Windows because it saves a lot of time when you need to run a batch of commands frequently. With a batch file, you save all the commands into one file, and just run the batch file, instead of your gazillion commands individually.

I was facing the same situation in Mac OSX when I realised that I didn’t know how to create a batch file in Mac OSX. Turns out it’s pretty easy. Mac OSX is unix-based, so I could use the unix equivalent (which is called a script too). What you need to do is to put all the commands you want into a plain text document, and save it with a name (without the .txt extension preferably, but that really doesn’t matter…it just looks more right that way).

In Windows, that’s all that you need to do, but for the Mac, you’ll need to make sure that you edit your batch file’s permissions so it is executable. So for example, if your batch file is named batchfile, one way to change its permissions is to right-click on it, click on “Show Info”, and then change the permissions under “Permissions” to show 755.

What 755 does is to give permissions of 7 to you, 5 to your user group, and 5 to everyone else. With a permission of 7, you can write to the file and execute it. With a permission of 5, you can execute the file but not write to it.

Another way is for you to change permissions of the file is to go into Terminal, and enter this command that changes its permissions. You’ll need to be in the directory that batchscript is in for the following command to work (or you’ll need to specify its full path):

chmod 755 batchscript

Now to run your batch file, you just need to either specify the full path to the batch file, or if you are already in the directory where it is located, you can type:

./batchscript

Note that you have to put the “./” in front of your filename, in order to tell Terminal to look for the file in the current directory.

And with that, you’ll have a working batch file in Mac OSX!

Alvin Poh

I've over 17 years of entrepreneurship experience, having built a tech company to be the top in the country, which was subsequently acquired for an 8-figure sum. As CEO, I led the company through robust growth to 150 employees across 4 countries. After the acquisition, I embraced minimalism, sold off my personal possessions, and started living around the world. Read more about my story here.

View Comments

  • > In Windows, that’s all that you need to do, but for Mac there’s 2 extra steps

    2 extra steps?

    So I have to do *BOTH* of those 2 things. (Change the file permissions twice?)

    Why?

  • Thank you Alvin for this short and to the point article. Everything just works. I used to make batch files all the time in DOS/Windows. With your help I now have a batchscript to restart my local MySQL server if it looks like it's getting unstable.

    Again Thanks,
    Jim

  • hi

    how to handle password.For example i have like ssh c1557004@neviss

    and after that it prompts for a password which i want to add to the batch file only so that my other commands which are present after this command can run also.

    Waiting For your reply.
    Thanks.

  • not pleasing
    now what i use is neither pleasing that's why i was looking for a better solution.
    i take a hex editor to create the batch and it works later like a executable, i can start it with a doublclick.
    this sytem is ok for me, but you can't expect of everbody to use a hex editor even if that's for such a simple task the same as with any text editor.

  • When i drag and drop the batch file to the terminal it says:
    line 1: @echo: command not found
    installer.bat: line 3: rem: command not found
    line 4: rem: command not found
    line 5: rem: command not found
    ine 6: rem: command not found

  • Thanks a lot, It's working

    But it's not showing which command run and completed like windows.
    cd
    cd

    How can I see all the activity done?

    I am also posting the same blog on my website with your website link as reference

    • Use

      set -x

      If you include that at the top of your shell script, it's just like "echo on" in Windows.

  • I don't see any mention of "sudo" being needed with chmod. In my case, it was.

    chmod 755 test
    chmod: Unable to change file mode on test: Operation not permitted

    After running it with sudo once...
    sudo chmod 755 test

    ...it works fine with or without sudo on this or any file.

    I liked Windows XP better.

  • I have one BAT file when open as few options and by entering the options, I can access that particular file like prop files and launch my application for that particular agency, in the same way I need file on MAC Machine which can be accessed by following the above steps. My Bat File has some code and paths in it which points to C Drive and some folder in Program Files, so what can be done to the path and how can I define the some specific folder in MAC Book Pro

    If I paste the folder on MAC Desktop, can then I access the prop files from that folder and create a file for that which is similar to bat file of windows?

    If I share few lines of code from my Windows Bat file will that be helpful?

Recent Posts

Best Protein Bar Review: Which Is The Healthiest Of Them All?

Protein bars have become the go-to snack for fitness enthusiasts, busy professionals, and health-conscious snackers…

2 days ago

What Salary Raise To Give Your Philippines Team

A salary raise can significantly enhance the motivation and loyalty of your Philippines team members.…

2 weeks ago

3 Myths About Venture Capital and Entrepreneurship

The narrative of young entrepreneurs securing vast sums from venture capitalists for their startups, particularly…

3 weeks ago

Ultimate List of Top 80 Sites to Submit Your Startup to Promote It in 2024

Launching a startup is challenging. Despite many founders having innovative ideas, only a handful successfully…

3 weeks ago

Why Retirement Isn’t What It’s Cut Out To Be

What is retirement after all? A better way of looking at retirement is that it’s…

4 weeks ago

Fix ElementsKit Mega Menu On Mobile Problems

ElementsKit has a very simple and easy-to-use Mega Menu for Elementor, but it has a…

7 months ago