Table of Contents
This article will show you how to Make Bash Script Executable on Mac. A Bash script is a simple text file that has a list of tasks in it. These words are a mix of things we would normally type on the command line, like ls or cp, and things we could type but usually don’t. Bash scripts are files that tell your computer what to do by using code.
They’re a staple of the Linux world, and there are thousands of them freely available on the internet. You can also use these apps on your Mac by making a few changes. You can put any command you can run on the command line into a script, and it will do the same thing. If you want to know more information about this Visit Official Apple Support site.
In the same way, anything you can put into a script can also be run normally on the command line, and it will do exactly the same thing. You don’t need to do anything different. Just type the orders like you normally would, and they will work like they normally would. We just don’t type them at the command line anymore. Instead, we now type them into a plain text file. In this way, you already know a lot about Bash programming if you know how to do things at the command line.
How to Make Bash Script Executable on Mac
- Add the following line to the very top of your bash script, before any of the code:
!/usr/bin/env bash
- Make the script executable by changing the file permissions. Run the following command using a Terminal application:
chmod +x your-script
- Depending on how you made the bash script, you might need to change the default application that starts it. Select the file in the Finder, and then go to the File menu and choose Get Info. Make sure that a Terminal tool is chosen in the “Open with” section.
Running a Bash Script
- Now you can run your bash script! To start it, simply double click it in the Finder. Or, in a Terminal application, run the following command:
./your-script
- That’s all there is to it!
What is a Bash Script?
Bash is a Unix command line tool that lets you talk to the operating system of a computer. A bash script tells the bash shell what to do, just like a movie script tells the players what to do. Thus, a bash script is a useful way to group commands to build a program.
Any command you could run directly from the command line can be put into a bash script, and you could expect it to do the same actions as it would from the command line. You could also run any program you put into your bash script directly from the command line and get the same result. In other words, if you know how to use the command line to talk to your computer, you already know a lot about bash programming.
One of the biggest differences is that a bash script is a plain text file that is usually saved with the.sh extension, though it can still work without it. The “shebang” on the first line of all bash files is another thing that sets them apart. Essentially, the shebang is an absolute path to the bash interpreter made up of a mix of bash “#” and bang “!” followed by the bash shell path. It tells the Linux operating system which translator to use to read and understand the file.