Learn by reading through in order

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.

Create → grant permission → run, and how ls -l changes
Write with vihello.sh created-rw-r--r--Check permissionwith ls -lAdd exec permission-rwxr-xr-xx appearsWith x set, runit via ./Run the scriptrun info: etc.is printedvi hello.shchmod +x hello.sh./hello.sh
Creating it with 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.
SyntaxMeaningExample
vi fOpen a file in the editor (create it if it doesn't exist)vi hello.sh
iEscWrite the body in insert mode, Esc returns to normal modePress iEsc
:wqSave and quit vi:wq
#!/bin/shThe shebang on the first line (specifies the shell to run)#!/bin/sh
# commentAn explanatory line ignored at runtime# create backup
chmod +x fAdd executable permission to a filechmod +x hello.sh
./fRun the script in the current directory./hello.sh
>> fAppend 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

You'll build a small tool that records the date you ran it and the working directory.

① Open hello.sh with vi hello.sh and press i to enter insert mode.

② On the first line write #!/bin/sh, then add a comment line stating what the script is for and the commands that print the current date and working directory (you can copy and paste the contents from the answer panel).

③ Press Esc and save and quit with :wq.

④ Add executable permission to the hello.sh you created.

⑤ Run ls -l hello.sh and confirm that an x appears at the left edge, showing the executable permission changed.

⑥ Run the script and confirm that the date and working directory are printed. (If it runs correctly, an explanation will appear.)

Linux console
0 / 4 completed
Loading Linux Terminal...

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.

You'll go through the flow of creating a script, adding executable permission, and running it.

① Open run.sh with vi run.sh, write #!/bin/sh on the first line and a command that prints a short message on the next line, then save with :wq.

② Check the current permissions with ls -l run.sh (there's no executable permission yet).

③ Add executable permission to the run.sh you created.

④ Run ls -l run.sh again and confirm that an x appeared at the left edge.

⑤ Run the script with ./ and confirm that the message is printed.

Linux console
0 / 4 completed
Loading Linux Terminal...

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.

The two actions of who.sh and the buildup of history
Run who.shAppend one line oftime to run.log>> adds to the end,not overwriteShow all linesof run.logRun itonce morerun.log grows to2 then 3 linesRun historypiles update >> run.logcat run.log./who.sh
A single run causes the date >> run.log append and the cat run.log display, and each repeat piles up another line in run.log.

You'll build a small tool that records the time you ran it to a log file.

① Open who.sh with vi who.sh, press i to enter insert mode, and write #!/bin/sh on the first line.

② Add a comment line stating what the script is for, append the current time to a log file (>>), and then write a command to show the whole log.

③ Press Esc, save with :wq, then add executable permission to the file.

④ Run the script and confirm that the recorded line appears in the log.

Linux console
0 / 3 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1What does the #!/bin/sh on the first line of a shell script specify?

Q2Which command adds executable permission to a script you created?

Q3Which is the way to run hello.sh in the current directory?