How to run .sh file in Mac Terminal


A file with .sh extension is a Bash script that you can execute using the Mac Terminal. Say you have a file script.sh and you want to execute it, follow the below steps.


Step 1: Make sure the sh file has execute permissions

    By default the sh file will not have execute permissions, we can check that using the ls command.

    % ls -l script.sh 
    -rw-r--r--  1 c2ctech  staff  36 Sep  6 12:11 script.sh

    If we try to run the sh script without execution permission we will get an error.

    % ./script.sh
    
    zsh: permission denied: ./script.sh

    So before we can execute the script we need to add execute permissions using the command chmod +x

    Example:
    chmod +x script.sh

Step 2: Running the .sh script file

    We can run the sh file by adding a ./ before the script file name.

    Example:
    ./script.sh

    If you are providing an absolute path for the file, you can do that as follows,

    /Users/c2ctechtv/Desktop/script.sh
    Execute .sh file in Mac Terminal example

This is not an AI-generated article but is demonstrated by a human on an M1 Mac running macOS Sonoma 14.0.

Please support independent contributors like Code2care by donating a coffee.

Buy me a coffee!

Buy Code2care a Coffee!

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap