Why can’t I chmod to make a file executable without sudo

pathpermissionsudounix

On OS X 10.6.8, I made a small script file in my home directory and tried to make it executable. Surprisingly, I found that chmod had no effect until I ran it as super-user. I own and have write access to my home directory directory and the script, so what prevents chmod from working?

my_mac:~ crowding$ ls -dle ~
drwxr-xr-x+ 214 crowding  staff  7276 Jul 28 16:30 /Users/crowding
 0: group:everyone deny delete
my_mac:~ crowding$ ls -le ~/my_script 
-rw-r--r--  1 crowding  staff  22 Jul 28 16:25 /Users/crowding/my_script
my_mac:~ crowding$ chmod a+x ~/my_script 
my_mac:~ crowding$ ls -le ~/my_script 
-rw-r--r--  1 crowding  staff  22 Jul 28 16:25 /Users/crowding/my_script
my_mac:~ crowding$ sudo chmod a+x ~/my_script 
Password:
my_mac:~ crowding$ ls -le ~/my_script 
-rwxr-xr-x  1 crowding  staff  22 Jul 28 16:25 /Users/crowding/my_script
my_mac:~ crowding$

Best Answer

It turns out there was another exec "chmod" on the path, obscuring my own chmod.

The thing that made this difficult to see was that I'd used the metacharacter "~" in my PATH variable. Only some things will interpret a tilde in the PATH. The which program does not expand the tilde, so which chmod told me that /bin/chmod was the chmod on the path, but bash does expand the tilde, so it tried to run the other chmod (which did nothing.) Meanwhile, sudo does not expand the tilde so it worked, and gave the impression of a permissions problem.

Resolution: when adding subdirectories of your home directory to your PATH, use $HOME instead of ~.