(1)
echo "value is $foo"
echo 'value is $foo'
variables in single quote will not be replaced.
(2)
You can define a function in a .sh file and then source it. Then you can use this function in your terminal.
(3)
$? will get you the error code from the previous command.
$_ will get you the last argument of the previous command.
(4)
in shell, `!!+tab` = your previous command.