The Official SPUG Bash Tutorial

1 view
Skip to first unread message

jda2000

unread,
Nov 21, 2009, 2:45:08 AM11/21/09
to Space Port Computer Users Group
Here are some instructions you can use to create and run your own bash
script
without having to use vi:





1|$ echo -n '#!' >hello.sh

2|$ which bash >>hello.sh

3|$ echo 'echo "Hello world!"' >>hello.sh

4|$ chmod +x hello.sh

5|$ ls -l hello.sh

6|-rwxr-xr-x 1 jda2000 jda2000 32 2009-11-20 23:19 hello.sh

7|$ cat hello.sh

8|#!/bin/bash

9|echo "Hello world!"

10|$ ./hello.sh

11|Hello world!





The lines are numbered for reference.



Ignore the vertical bars, they represent the left margin of your
terminal window.



Lines starting with "$" are the ones you type. The "$" represents the
input-prompt

for your terminal session and will most likely include other
characters such as your

user and system names before the "$". In short, you don't type the
"$".



The other lines, 6, 8, 9 and 11 are what the computer prints in
response to your commands.



Line 1 creates a file named "hello.sh" that contains only two
characters. "#" and "!".

The echo just prints (echos) whatever else you type on the same line.
The "-n" option

says to skip the usual end-of-line character in echo's output. The
">" tells bash

that you want the output to be redirected into the following file
name. In this case,

"hello.sh". The file will be created if it does not exist. The single
quotes around the

"#!" characters indicate that any special meaning that those
characters might have should

be ignored for now. Bash usually does not interpret the "!" as an
ordinary character.



Line 2 appends the full name of the file that contains the bash
command to the file named

"hello.sh". The "which" command prints the location of whatever
command follows it.

Appending the location of the bash command file completes the first
line of the script

we are building. You can see the result on line 8. The ">>"
redirection symbol is like the
">" symbol except that it adds to rather than over-writes the output
file content.



Line 3 appends an echo command to the file named hello.sh. This
becomes the second line

of your script and can be seen on line 9. Again, the single quotes
are used to defeat any

special handling of the enclosed characters. Notice how they don't
appear on line 9.



Line 4 tags the file named "hello.sh" as an executable file. The
chmod command can also

set or remove read and write permissions for a file. By setting the
execute permissions

for the file "hello.sh" you are indicating that you want that file
name to be treated as a

new command.



Line 5 causes information about the file "hello.sh" to be printed.
The result is shown on

line 6. The file "hello.sh" contains 32 characters (including end-of-
line characters in addition

to the ones you can see here). The owner of the file can read write
or execute it.
Others can
read or execute the file but not write to it. (Only the owner can
change or delete it.)


Line 7 causes the contents of the file "hello.sh" to be printed on
your screen. The cat

command is often used for this purpose though it has other uses too.



Line 8 and 9 are your script. The first line says that the program /
bin/bash should be

used to execute the commands in the rest of the file. The characters
"#!" at the very

start of the first line of the file indicate that the name of the
intended script

interpreter follows. It is possible that this will be a different
file name on your

system. If you have no file name after the "#!" then bash is not
installed on your

system and the script will not run.



Line 10 runs the script. You may have noticed that the first thing on
every line that

you type is a command. A command is usually the name of a file that
contains a program

(or script) that does some useful work. More information about the
built-in commands

used in this tutorial can be viewed by typing in the command "man"
followed by the

name of the command. For example: "man echo" requests explanatory
information about

the echo command. Similarly, this tutorial used the command "which
bash" to request

the full file name of the bash program. You usually don't have to
type in the full

file name of a command. The shell has rules for finding the right
executable file.

The which command shows you the result of that search. On line 10 the
notation

"./hello.sh" indirectly gives the location of the hello.sh file to
use. The "."

character indicates that the current directory should be searched.
This is required

if the rules for finding a command on your system do not specify a
search of the

current directory.



Line 11 is the result or output of the execution of your script.
Usually you would

have more than just one echo command in a script and the output from
all the commands

executed would comprise the output of your script.

jda2000

unread,
Nov 21, 2009, 3:20:30 AM11/21/09
to Space Port Computer Users Group
Ok, that got pretty badly mangled. I put a PDF version in the files
section of this group.
Reply all
Reply to author
Forward
0 new messages