Remember, no spaces around the equal sign
Otherwise the shell would think the lhs is a command
Refer to a var with a $ in front of it:
echo $a
Just remember that since everything in bash is a string, we need the dollar sign to indicate a variable reference
#
Use the full syntax for a variable reference, which includes not just the dollar sign, but also braces around the variable name:
somescript /tmp/rep${FN}port.txt
Export variables that you want to pass on to other scripts:
export SOMEVAR #basically make it a env var
#var.sh
#!/bin/bash
myvar="IT3110"
export myvar
./myscript.sh
#somescript.sh
#!/bin/bash
echo "my var is ${myvar}"
exit 0
env
shows all exported variables. (Environment variables)set
shows all shell variables (only available to current shell)Try:
foovar=3
env | grep foovar; set grep foovar
export foovar
env | grep foovar; set grep foovar
All strings present after the command invocation will be stored as numeric variables within the script.
./somecmd i like peaches
#somecmd
echo ${1}
prints an 'i'
What would you change the script to, to print out 'peaches'?
You can use a $*
to refer to all command line arguments.
You can use a $@
to be more robust (see 5.10 in book for more info)
Default values:
foo=${1:-something}
#gets first command line argument if there, otherwise "something" a=3
name='tom'
user=$(cat /etc/passwd | grep 1000 | awk '{ print $1}')
mydate=$(date)