Making Scripts Executable: Run Them Directly
To run scripts directly, they need execute permission.
Here's the thing: Scripts need execute permission. Give it to them. Then run them.
Making Executable
chmod +x
chmod +x script.sh
My take: chmod +x makes scripts executable. Use it.
chmod 755
chmod 755 script.sh
My take: 755 is common. Owner: read/write/execute. Others: read/execute.
Running Scripts
With Execute Permission
./script.sh
My take: ./ runs the script. Needs execute permission.
Without Execute Permission
bash script.sh
My take: This works without execute permission. But ./ is better.
Common Patterns
Make and Run
chmod +x script.sh
./script.sh
One Liner
chmod +x script.sh && ./script.sh
Common Mistakes (I've Made These)
-
Forgetting chmod: Scripts need execute permission. Don't forget.
-
Not using ./: Use
./script.shnotscript.sh. Path matters. -
Wrong permissions:
755is good.777is too open. Don't use it. -
Not checking permissions: Check with
ls -l. See what you have.
What's Next?
Now that you can make scripts executable, you're ready. Or review Script Structure to write better scripts.
Personal note: I used to forget chmod. Then scripts wouldn't run. Now I always chmod +x. It's habit. Do it.