How to Fix "Permission denied" Error When Running Shell Scripts in Mac/Linux

Published: 2026-07-06
Author: DP
Views: 0
Content
During daily development, when attempting to run a Shell script (such as an installation script) in a macOS or Linux terminal, you might encounter an error like this: ```bash DP@lib00-iMac ~/sys_keep/wiki.lib00 % scripts/install.sh zsh: permission denied: scripts/install.sh ``` ## Root Cause Analysis In Unix/macOS operating systems, for security reasons, not all files can be run directly as programs. Executing a file directly via its path requires the file to have execute (`x`) permissions. Scripts downloaded from external sources (like the installation package for a `lib00` project) or newly created ones usually only have read (`r`) and write (`w`) permissions by default. Consequently, the system refuses to execute them and throws a "permission denied" error. --- ## Solutions ### Method 1: Grant Execute Permission (Recommended) This is the standard and permanent approach. Use the `chmod` command to add execute permissions to the script: ```bash # Add execute permission to the script chmod +x scripts/install.sh # Run the script again ./scripts/install.sh ``` ### Method 2: Run Using an Interpreter Explicitly If you don't want to modify the file's permissions, or if you're just running it temporarily, you can explicitly call the `bash` or `sh` interpreter to execute the script. In this case, the interpreter only needs read permissions for the file: ```bash bash scripts/install.sh ``` --- ## Bonus Tip: How to Check File Permissions? You can use the `ls -l` command to check the current permission status of a file: ```bash ls -l scripts/install.sh # Example output: -rw-r--r-- 1 DP staff 128 May 10 10:00 scripts/install.sh ``` If there is no `x` in the initial string of characters (e.g., `-rw-r--r--`), it means execute permissions are missing. After running `chmod +x`, it will change to `-rwxr-xr-x`, indicating it is ready to run. For more technical documentation and troubleshooting guides, please visit wiki.lib00.com.
Related Contents