Bash programming help

Shell Variables

Shell variables are set using the form var=value. Note that there can't be any space around the = sign. Environment variables are supported by exporting shell variables.

bash$ echo $v

bash$ v=12
bash$ echo $v
12
bash$ printenv v
bash$ export v
bash$ printenv v
12
bash$ bash
  bash$ echo $v
  12
  bash$ v=14
  bash$ echo $v
  14
  bash$ printenv v
  14
  bash$ exit
  exit
bash$ echo $v
12

Control flow

We can do everything with if statements. The general form is shown below in the shell program. Additional tests on files are given following.

#!/bin/bash

# Set some local (shell) variables
v=12

if [ $v = 12 ]; then
  echo The variable is twelve
else
  echo The variable is $v
fi

if [ -op filename]; then
  expr
fi
opTest
rReadable?
wWritable?
xExecutable?
eExists?
OIs owner of?
fIs plain file?
dIs directory?
LIs symbolic link?

Repetition

Looping is important. The form for looping in bash is:
for var in list
do
  statements
done

For example, the script:

for n in 1 2 3 4; do
  echo $n
done

produces

1
2
3
4

corin@cs.washington.edu