Blog Post

How to make a simple bash script (Mac)

Mac OS X menu dropdown selection image

 

How to make a simple bash script (Mac)

 

The first step to make a simple bash script is writing the script.

Open Text Edit, found in Applications, once in Text Edit, click “New Document”.

Next, write the Bash Script, as below:

#!/bin/bash      tells the terminal that you are using bash shell

echo hello world    prints out “hello world” in the terminal

Once you have written the script, you have to convert the document into plain text.

Select “Format” from the Menu and then click “Make Plain Text.” 

Next click “File” and then “Save.”

Name it whatever you would like, but remember how you typed the name because we are going to be using that exact name in the terminal.

For the purpose of this tutorial, I am going to name it “FirstScript”, and I am going to save it in my Documents folder.

Also you have to uncheck the box that says “If no extension is provided, use ‘.txt’.”

Next you are going to open the Finder.

Search for the name of the script you just wrote, or navigate to the file.

Once you found it, right click on it (CTRL + click) and click “Get Info”.

 

 Look on the very bottom right of the opened info window, and you will see a Lock icon that should appear locked.

Click it, and if you have a password on your computer it will ask you for your password to unlock it, otherwise it will just unlock.

Once that is done, you have to open Terminal and navigate to the folder you put the document in.

In the terminal, for my case, I am going to type in

cd documents

Now we have to change the file we saved to an executable file. Type in chmod 700(file Name). I’m going to type in

chmod 700 FirstScript

This will change it into an executable file.

Now type in ./(File Name). Im going to type in

./FirstScript

You should see “Hello World” printed out.

357

2 comments

Just adding onto this, since Mac OS is based of Unix you can apply this tutorial to Unix or Linux based operating systems. In the link below you have a couple of popular text editors with descriptions about them. They are very useful as they explain what operating system they are for and what language they have been written in. You would just use the text editor in place of Mac's TextEdit and just follow the tutorial from then.

 

Here - http://www.thegeekstuff.com/2009/07/top-5-best-linux-text-editors/

313

This is perfect.  Thank you for posting this. 

172