Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

chmod +x

chmod +x backup.sh
ls -l backup.sh
-rwxr-xr-x 1 deploy deploy 412 Nov 30 11:02 backup.sh

An x now appears in the owner, group, and others sets. Without a letter in front, +x adds execute for all three, except for bits your umask blocks. You must own the file or use sudo. Everything else about modes is in the chmod command guide.

u+x or a+x

CommandWho may run it
chmod u+x fileOwner only
chmod ug+x fileOwner and group
chmod a+x fileEveryone

For a script that contains passwords or keys, give execute to the owner only and remove read for others as well.

Numeric Modes: 755 and 700

chmod 755 deploy.sh
chmod 700 deploy.sh

755 lets everyone read and run the file and only the owner change it. 700 keeps it to the owner. A script also needs read permission for whoever runs it, because the interpreter reads the file; a compiled program needs only execute.

Run It with ./

./backup.sh
/home/deploy/backup.sh

For a bare name, the shell searches only the directories in PATH, and the current directory is not one of them, on purpose. Typing backup.sh alone therefore gives "command not found" (see command not found). The ./ says "this directory". To run a script by name from anywhere, put it in /usr/local/bin, or in ~/.local/bin when that directory is in your PATH.

The Shebang Line

#!/usr/bin/env bash
echo "Backup started"

The first line tells the kernel which interpreter runs the file. Without it, the result depends on who starts the script: bash runs it itself, other programs may hand it to /bin/sh, which is dash on Debian and Ubuntu, so bash features fail with odd syntax errors, and cron or systemd can fail with "Exec format error". Python scripts use #!/usr/bin/env python3. Writing scripts is covered in bash script, and the shell itself in what is bash.

Permission Denied After chmod +x

  • No execute bit: ls -l shows no x for you; run chmod again, as the owner or with sudo.
  • A noexec mount: the file system holding the file was mounted with the noexec option, which some systems use for /tmp, /dev/shm, or network shares. Check the options with findmnt, and move the script elsewhere. Do not remount a system file system without knowing why noexec was set.
  • A directory on the path: every directory on the way to the file needs x for you. namei lists the permissions of each part of the path.
findmnt -T ./backup.sh -o TARGET,OPTIONS
namei -l /opt/tools/backup.sh

CRLF Line Endings and Bad Interpreter

A script saved with CRLF line endings carries an invisible carriage return at the end of each line. The kernel then looks for an interpreter named bash followed by that character. With #!/bin/bash, the error mentions a bad interpreter, often with ^M after the name; with #!/usr/bin/env bash, env reports that it cannot find bash, with a stray \r in the name. Confirm it with file backup.sh, which reports CRLF line terminators, or cat -A backup.sh, which shows ^M at line ends.

The fix rewrites the file, so keep a backup copy. sed with -i.bak does that, or install the dos2unix package:

sed -i.bak 's/\r$//' backup.sh
dos2unix backup.sh

Run a Script Without the Execute Bit

bash backup.sh
sh install.sh
python3 tool.py

Calling the interpreter directly needs only read permission, and it also works on a noexec file system, because the program that runs is the interpreter. The shebang line is ignored in this case, so choose the interpreter the script was written for.

Directories Need x Too

On a directory, x means you may enter it and reach the files inside. A directory with r but no x lets you list names but not open anything, which is why directories normally have 755 or 700. Running chmod +x on a directory does not make the files in it executable.

Was this answer helpful?
Loading...