compsci-missing_semester_2020/_subsections/lesson-02.org
2024-12-12 18:30:39 +02:00

3.6 KiB
Raw Blame History

lesson 02 | course overview + the shell

Notes

bash

spaces are critical with bash

  • this works:
foo=bar
echo $foo
  • this doesn't:
foo = bar
echo $foo
  • the output of that will be zsh: command not found: foo
  • what happens in the above example is that we are effectively calling the "foo" program with the arguments: "=" and "bar"

quotes

  • you can use double or single quotes to print a value

    echo "Hello"
    echo 'Hello'
  • double quotes can interpolate variables

    echo "Value is $foo"

    will return: Value is bar

  • single quotes can NOT interpoloate variables

    echo 'Value is $foo'

    will return: Value is $foo

sequential execution

bash functions

  • mcd.sh
mcd() {
        mkdir -p "$1"
        cd "$1"
        }
  • creates a function that can be executed after loading

    • $1 is a global variable referring to the first parameter

source

  • using source mcd.sh source mcd.sh
  • then carry out the function mcd testdir
  • this will create a new directory and cd into it as per the definied functioned

variables

  • $0 name of the script we are currently in

    • if you run this in the shell it will display 'bash'
  • $1->$9 the first through ninth argument given to a script
  • $? the last error message
  • $_ — last argument of the previous call
  • !! recreates the last call with it's arguments

    • use with sudo if you found your call needed a sudo and you don't feel like retyping it again
    ls -lah
    sudo !!