1/30
Looks like no tags are added yet.
how to declare and call upon a variable
how are vars stored as? what happens if multiple words in the var?
decl: var=”variable” , call with $var .
they’re stored as strings. if multiple words, store in quotes, otherwise there’ll be errors.
what are environment variables and four examples?
how do you find more?
they’re vars created by our system.
examples: HOME</code>,(pathtohomefromroot)<code>LOGNAME (username of logged in user), RANDOM</code>(randomnumbetween0and32000),and<code>SECONDS (seconds since sessions tart).
we find more by using env command
variable expansion: length
${#var}
var expansion: slicing a part in the middle
${var:FROMINDEX:LENGTH} , where length includes the character at start index.
using double quotes vs single quotes to store variables
anything in single quote doesn’t allow variable resolution (so if you have a lil $var in there, it’ll print it verbatim instead of treating it like a variable)
variable expansion: slicing from index to the end
${var:2} slices from pos 2 to end.
how to take user input
read var stores it in var
what does shebang do?
tells linux to use the bash interpreter to run the script
what does this do: echo ${goon}2 if goon=”hello”
this would output hello2
which expansion is performed before all other expansions?
brace expansion
what does tilde expansion do? what does the tilde have to have?
replaces the value of ~ (must be UNQUOTED) with home directory pathname.
what are two ways to save values of commands inside variables?
var=`command`
and
var=$(command)
how to evaluate arithmetic expressions?
$((expression))
what are the 5 recognized arithmetic operators
+, -, /, *, %
what characters are a part of filename expansion?
* which matches any string including null str
? which matches any single character
[…] which matches any one of the enclosed characters
format of conditional statements?
if [ expression ]; then
stmt
elif [ expression ]; then
stmt
else
stmt
fiwhat are numerical comparison thingers? (hint: 4 “than” comparisons; 2 “equal” comparisons)
-lt, -gt, -le, -ge, -eq, -ne
what are the string comparison thingers? (4)
= if two strings are equal
!- if two strings are not equal
-n evaluate if string length is greater than zero
-z if string length is equal to zero
logical operators and format
if [ expr ] && [ expr ]
if [ expr -a expr2 ]
if [ expr ] || [ expr ]
if [ expr -o expr2 ]
for loop notation
for item in ITEM1 ITEM2 ITEM3
do
meow
doneor
for item in {1..5}
do
meow
donehow to use seq for for loops
basically range
for item in $(seq startnum endnum)
# could also be a var.
for item in $(seq 0 $max)DANGER: it is inclusive for the end num, unlike range.
what is the non “seq” way to use range in for loops
{start..end} or {start..step..end}
while loop structure and two keywords inside it
structure:
while [ condition ]; do
stmt
donecontinue skips remaining part in current iteration and jumps to the next one
break terminates the loop
for arrays in bash, what are its limits and requirements on contiguous assignments
there are none for either.
what are the three ways to make an index array
declare with strings inside (space separated NOT comma separated) pet=(cow “a horse” “donkey”)
make by putting in elements pet[2]=”meower”
brace expansion: pet=(a{1..3})
extracting one element for an array and extracting ALL elements for an index array?
one elem: arrayname[i]</code></p><p>allelem:<code>{arrayname[@]}
how to know how many elems there are in an index array?
${#arrayname[@]}
how to extract all assigned indices of an index based array
${!arrayname[@]}
how to extract only some elements from an index based array
{arrayname[@]:startpos where it extracts values starting at index startpos all the way to the end.
{arrayname[@]:startpos:length where it extracts values starting at index startpos and total of length indices.
unquoted array vs quoted @ array vs quoted * array
unquoted: arrayname[@]</code>and<code>{arrayname[*]} just extracts all content using space to divide it, so multi-word strings in your array are NOT preserved.
quoted *: ”arrayname[∗]”</code>extractsallcontentinaSINGLEstring.entirearrayisinonestring.bashtreatsitasonestring,eveninforloopssoitrunsonlyonce.</p><p>quoted@:<code>”{arrayname[@]}” extracts all content as elements in strings. multi-word strings are preserved.
howto add new elements to index based array?
how to del elements from index based array?
arrayname+=(elem1 elem2 … elemN)
do NOT ignore parenthesis, otherwise it just concatenates your input to the back of the first element in the array
delete: unset arrayname[index]