CS 279 - Week 3 Lab - 2022-09-08
NOTES built to POST (since could not figure out
how to project from lab on 2022-09-08...!)
=====
another way to reference the value of a shell variable
=====
* you know you can use $ to reference the value of
a shell variable:
> declare name=David
> echo The string value is $name
The string value is David
* you can ALSO use ${} to reference its value:
> echo The string value is ${name}
The string value is David
* an example where ${} can be handy:
...when there might be CONFUSION about exactly
where the variable name ends;
> name1=stuff
> echo $name1
stuff
> echo ${name}1
David1
=====
double quotes and single quotes and shell variables
=====
* Consider: sometimes you want a blank where it would otherwise
confuse the shell -- for example, a multi-word greeting:
> greeting=Hi there
-bash: there: command not found
* putting the desired text in double quotes OR single quotes
solves this problem:
> greeting="Hi there"
> echo $greeting
Hi there
> greeting='Hello mate'
> echo $greeting
Hello mate
* BUT: there is an IMPORTANT DIFFERENCE between
single and double quotes if the contents include
a shell variable!
* if a shell variable is in double quotes - WILL
parse variable, use its value!
(In another language, I've heard this called
variable interpolation...)
* if a shell variable is in single quotes - WON'T
parse variable, you'll see the variable name!
* SO:
> echo "Now you see: $greeting"
Now you see: Hello mate
> echo 'Now you see: $greeting'
Now you see: $greeting
=====
and you can escape the special meaning of $ with \ also
=====
* ...what the heading says!
This also keeps the variable from being parsed by the shell:
> echo \$greeting is: $greeting
$greeting is: Hello mate
=====
using backquotes to set a variable to a command's result
=====
* because sometimes you want to use the result of a
command later (in the shell or in a shell script!)
* IF you surround a shell command with backquotes,
its result will be assignable to a variable
(instead of printed to the screen)
> declare here=`pwd`
> echo \$here is now: $here
$here is now: /Users/smtuttle/humboldt/f22cs279/279labs/279lab03
> curr_contents=`ls`
> echo $curr_contents
279lab03-ex.odt 279lab03.odt fruit-clicker
* this can be quite useful, as I hope we'll see as we continue
the semester!
=====
let command - to treat shell variable contents like numbers!
=====
* When setting or changing numeric values in variables,
the let command is used
> let val=10
> let val=${val}+5
> echo $val
15 # added 5 to 10, treated as numbers
* Without the let, treats the variable's value as a string:
> val+=1
> echo $val
151 # concatenated the 1, because no let used above
> let val+=1
> echo $val
152
* does ++ work in bash? ...looks like it does IF you use it
with let:
> val++
-bash: val++: command not found
> $val++
-bash: 152++: command not found
> let val++
> echo $val
153 # added 1! when let used with ++
* and: it appears you can use let even after initially
setting a shell variable:
> stuff=13
> stuff+=2
> echo $stuff
132 # no let, so appended
> let stuff+=2
> echo $stuff
134 # had let, so added