chmod, and Why Scripts Need +x

Changing permissions. chmod takes what to change and what to change it on, in two notations.

Symbolic is readable: chmod +x script.sh adds execute, chmod -w file removes write.

Numeric is compact, treating each group of three as a digit where read is 4, write is 2 and execute is 1, added together. So chmod 755 script.sh gives the owner 7 which is read write execute, and gives group and others 5 which is read and execute.

The number you will see most is 755 for programs and directories, and 644 for ordinary files. Those are not arbitrary. 755 means everyone can use it and only the owner can change it. 644 means everyone can read it and only the owner can edit it. Almost everything on a machine is one of these two.

✦ Permission denied on your own script

This is the most common permission problem there is. You write a script, you run ./script.sh, and the shell refuses. Nothing is broken. A newly created file has no execute bit, because letting every file you create be runnable would be dangerous. chmod +x is you stating that this particular file is meant to be a program.

SANKOFA SANDBOX
learner:~$

scripts/greet.sh exists but was created without the execute bit, exactly as a real one would be.

Try it → Run `./scripts/greet.sh` and read the refusal. Then `ls -l scripts`, then `chmod +x scripts/greet.sh`, then `ls -l scripts` again to see what changed, and finally run it.

About sudo. sudo runs a single command as the superuser, who is exempt from permission checks. It exists for genuine administrative work such as installing system software.

The thing to internalise is that a permission error is information. It is telling you that you are touching something outside your own space, and the right first response is to ask why rather than to repeat the command with sudo in front. Using sudo to silence an error you did not understand is how people damage their own systems.

◈ Graded Task

Make a script runnable. Create a file called build.sh in your home directory containing the single line echo Building, give it execute permission for everyone, and run it. Its permissions must end up as 755.

Checked against the filesystem you leave behind, not the commands you type, so any route that gets the job done counts. 4 visible and 2 hidden checks.

SANKOFA SANDBOX
learner:~$

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back