Variables Flashcards
how do you write a function?
name () {
commands
return
}
or
function myfunc {
echo “hello $1”
}
Usage:
myfunc “John”
What is top down design?
Process of identifying top-level steps and developing increasing detailed views of those steps.
Shell functions are:
“Mini-scripts” located inside other scripts and can as as autonomous programs.
Bash Shebang?
#!/usr/bin/env bash
How to make a script executable?
chmod +x [SCRIPT NAME]
./[SCRIPT NAME]
How are variables written:
String:
VARIABLE=”string”
or
Numerical Value:
VARIABLE=[NUMBER]
a=z
Assign the string “z” to variable a.
b=”a string”
Embedded spaces must be within quotes
c=”a string and $b”
Other expressions such variables can be expanded into a variable
d=”$(ls -l foo.txt)”
Variable is the result of the cmd
e=$((5 * 7))
Arithmetic expression
f=”\t\ta string\n”
Escape sequence such as tabs and new lines.
Variable result of: filename="myfile" #mv "$filename" "${filename}1"
myfile1
Quoting in variables:
Good practice to enclose variables and cmd substitutions in double quotes to limit the effects of word-splitting by the shell.
echo $LINENO = ?
This will print the line number in the script where this echo line was ran.
Probably helpful for writing your own error codes