Part 4 - Writing Shell Scripts

24 - Writing Your First Script

How to Write a Shell Script

  1. What a script #!/bin/bash (shebang)
  2. Make the script executable: 755 for everyone can execute, 700 for only the owner can execute
  3. Put the script somewhere the shell can find it: usually in /usr/local/bin

By using line-continuations(backslash-linefeed sequences) and indentation, the logic of command can be more clearly to the reader.

Configuring vim for Script Writing

:syntax on 
:set hlsearch
:set tabstop=4
:set autoindent # to stop indentation, type `Ctrl-d`

25 - Starting a Project

Variables and Constants

When the shell encounters a variables, it automatically creates it.

# this declares a read-only variable (constant)
declare -r TITLE="Page Title"
touch "$filename"
# mv "$filename" "$filename1"
# this avoids the shell intepreting the second argument as a new (and empty) variable
mv "$filename" "${filename}1"
ls=aaa # assign the string "aaa" to variable ls
echo "ls equals $ls"
echo ${ls} # print the variable ls
echo $(ls) # results of a command, one line
echo "$(ls)" # results of a command, multiple lines
echo $((5+7)) # 12
echo "aaa\nbbb" # escape sequences such as tabs and newlines

here document: is an additional form of I/O redirection in which we embed a body of text into our scritpt and feed it into the standard input of a command.

  • Single and double quotaes within here documents losed their special meaning to the shell
command << token 
foo="some text"

cat << _EOF_

26 - Top-Down Design

27 - Flow Control: Branching With If


Get a HTML version of bash manpage:

gunzip -c $(man -w bash) | groff -mandoc -Thtml > bash.html