Q1What does the #!/bin/sh on the first line of a shell script specify?
First Shell Script — Creating and Running It
Create a shell script in vi, add #!/bin/sh, make it executable with chmod +x, and run it with ./hello.sh. Illustrated and practiced in your browser.
Creating a Script — vi and #!/bin/sh
A shell script bundles the commands you'd normally type into a terminal into a file so you can run them all at once.
Instead of typing a fixed set of steps by hand every time, you write them into a file, and you can repeat the same work accurately as many times as you want.
Here you'll learn the basics of creating and running a shell script.
Open the file with vi hello.sh, press i to enter insert mode and type the body, then press Esc and save and close with :wq (the basic vi operations are the same ones you learned last time).
Make the first line #!/bin/sh, a shebang, which specifies which shell runs the script.
Lines that start with # are ignored at runtime as comments.
The only exception is the first line: even though #!/... starts with #, it isn't a comment — it's specially interpreted as the directive for which shell to run.
vi hello.sh gives -rw-r--r-- (no x); chmod +x changes it to -rwxr-xr-x (with x), and ./hello.sh runs it. The center shows checking with ls -l.| Syntax | Meaning | Example |
|---|---|---|
vi f | Open a file in the editor (create it if it doesn't exist) | vi hello.sh |
i … Esc | Write the body in insert mode, Esc returns to normal mode | Press i … Esc |
:wq | Save and quit vi | :wq |
#!/bin/sh | The shebang on the first line (specifies the shell to run) | #!/bin/sh |
# comment | An explanatory line ignored at runtime | # create backup |
chmod +x f | Add executable permission to a file | chmod +x hello.sh |
./f | Run the script in the current directory | ./hello.sh |
>> f | Append to the end of a file (without overwriting) | date >> run.log |
vi note.sh # open the script in the editor
# press i to enter insert mode and write:
# #!/bin/sh
# # show a short message
# echo "build step 1"
# echo "build step 2"
# press Esc and save and quit with :wq
chmod +x note.sh # add executable permission
./note.sh # prints build step 1 / build step 2
Running It — chmod +x and ./
A file you've only just created can't be run as is.
Once you add executable permission with chmod +x file, you can run it in the form ./file.
./ is the way to refer to a file in the current directory; without it, the shell only searches for the command within PATH and won't find it.
vi greet.sh # open in the editor, write #!/bin/sh and echo hi, save with :wq
ls -l greet.sh # no executable permission yet
chmod +x greet.sh # add executable permission
ls -l greet.sh # an x appears at the left edge
./greet.sh # prints hi
There's also a way to run it without executable permission
When you want to run it without adding executable permission, there's also a way to hand the file to a shell, like sh file.
In this course we use the basic form of adding executable permission and running with ./file.
You can check whether executable permission was added by whether an x appears at the left edge of ls -l.
Building a Small Tool That Records Runs
The create, grant-permission, and run flow you learned can be used directly in small real-world tools.
Here you'll build a logging tool that appends "when the script was run" to a single log file.
When you append with >>, lines pile up every time you run it, so you can look back at the run history later.
date >> run.log append and the cat run.log display, and each repeat piles up another line in run.log.Knowledge Check
Answer each question one by one.
Q2Which command adds executable permission to a script you created?
Q3Which is the way to run hello.sh in the current directory?