Linux Labs

Bash: text processing and scripts / Stage 4 of 9

A script is a file of commands

Not running Starting usually ~20 s
Leave the lesson

Step 3 of 6 · theory · 5 min

The x permission and lookup by name

./backup-notes.sh and plain backup-notes.sh are two different routes to the same file. The first runs into permissions, the second into PATH.

The kernel lets a file run as a program only with the x permission. backup-notes.sh has rw-r--r--: it may be read but not run — hence Permission denied. The owner gives themselves the right to run it with chmod: chmod u+x backup-notes.sh, where u is the owner and x is run. After that ./backup-notes.sh works: the kernel sees x, reads the shebang and starts /bin/bash.

The ./ at the start of the command is a path: "the file in the current directory". A name without / is looked up differently — in the PATH directories, as in the lesson “Help and the shell”. The current directory is not in PATH, so backup-notes.sh gives command not found even with the file right there.

Your own commands go into ~/bin. On Ubuntu, the file ~/.profile puts ~/bin at the front of PATH at login — if the directory already exists. A directory created in an open terminal stays invisible to that shell until the next login; to apply it at once, run source ~/.profile. A .sh extension is not needed to run a file, so a command in ~/bin gets a short name without it.

Check yourself 1 question

You ran chmod u+x backup-notes.sh. What will the shell answer to backup-notes.sh — without ./, in the same directory?

  • It never gets as far as checking permissions: the shell looks for the name in the PATH directories and does not find the file. type backup-notes.sh answers not found.

  • That answer comes from dash. bash would run a script with #!/bin/bash, but without ./ the shell does not even find the file.

  • The shell looks up a name without / in the PATH directories, and ~/script-lab is not among them: echo $PATH does not list it. It never finds the file, x or not.

The x permission decides whether a file may run; PATH decides where the shell looks for a name without /. The current directory is not in PATH, so the file is still not found by name, x or not. ./backup-notes.sh works, or a copy in a PATH directory.

Key points

  • ./file starts the file as a program: it needs the x permission, and the kernel takes the interpreter from the shebang.
  • A name without / is looked up only in the PATH directories; your own commands live in ~/bin, which ~/.profile adds to PATH at login.
Ubuntu 24.04
Loading…