How To Create a Bash Script in MacOS/Linux

Posted by Ray on September 19, 2020

This document is a straightforward guide for creating a bash script for Mac/Linux.

  1. Open up your terminal.

  2. Create your script file (example: exampleScript)
    1
    
    $ nano exampleScript
    
  3. Add a shebang line in the first line of the file.
    1
    
    #!/usr/bin/env bash
    
  4. Customize your script. In this example we echo “Hello World”.
    1
    2
    
    #!/usr/bin/env bash
    echo "Hello World"
    
  5. Exit and save changes in the editor by pressing “CTRL + X”, and “Y” then enter.

  6. Make the script executable by changing the script permissions.
    1
    
    $ chmod +x exampleScript
    
  7. Run script
    1
    
    $ ./exampleScript
    

Done!