bash open.sh runs a script, but you must know it's a bash script. To run a script by its path, like any program, give it a first line that names its program, and the x permission:
text
learner@reef:~$ cat close.sh
#!/bin/bash ← the shebang
echo "the shop is closed"
learner@reef:~$ ./close.sh
bash: ./close.sh: Permission denied ← no x yet
learner@reef:~$ chmod u+x close.sh
learner@reef:~$ ./close.sh
the shop is closed- The first line,
#!and a program, is the shebang. That program reads the file: its interpreter. Bash is one; Python is another (#!/usr/bin/python3). - The ending
.shdoesn't choose the program: only the shebang does. Without one, bash runs the file itself, which is wrong for any other language. So every script names its interpreter. - Write
./: the current directory is not in PATH, soclose.shalone iscommand not found.
| Line | Needs x | Who reads the file |
|---|---|---|
bash close.sh | No | bash, whatever the first line says |
./close.sh | Yes | the shebang's program; bash itself if there is none |
~/open.sh has one line and no x:
- With
nano, make#!/bin/bashthe first line of~/open.sh, above theecho. - Give the owner
x:chmod u+x ~/open.sh. - In your home directory, run
./open.sh.
Objectives
- Make
#!/bin/bashits first line. - Give the owner
x. - Run it by its path.