Skip to main content

0105 | Shells

Shells

Common Command Line Shells

  • korn shell | ksh
  • c shell | csh
    • popular with programmers
  • upgraded c shell | tcsh
    • popular with programmers
  • z-shell | zsh
    • more modern
    • default in kali
  • some commands like cd are built into the shell
    • like file and path expansions (* -- wildcard)
    • the shell first interprets the wildcard, then substitutes the names and sends them to ls
      • ls file*.txt

Environment Variables

  • each shell has it's own copy of the variables
    • changes in one, will NOT affect the others
  • list current vars in current shell
    • printenv
  • variable format
    • <var-name>=<var-value>
  • by convention var name is all UPPERCASE
  • PATH variable
    • the list of the directories that are searched in order to find commands that are to be executed
  • command execution in shell
    • 1 -- type the command
    • 2 -- shell checks if it's a built-in command
    • 3 -- if not built-in command, search PATH directories for the command
    • 4 -- if command exists in multiple directories, first is executed
  • global vs local environmental variables
    • global vars can be accessed by anything executing in that shell
      • like scripts and by and sub-shells
    • naming
      • global variables = environmental variables
      • local variables = shell variables
    • example | local variable
      # create local val
      COUNT_LOCAL=42
      echo $COUNT_LOCAL # will print the value
      echo COUNT_LOCAL # will print the string
      # creating subshell
      bash
      echo $COUNT_LOCAL # is not defined
      exit
    • example | global variable
      # create global variable
      export COUNT_GLOBAL=55
      echo $COUNT_GLOBAL
      # create sub-shell
      bash
      echo $COUNT_GLOBAL # var is defined
      exit

      # unset the variable
      unset COUNT_GLOBAL
      echo $COUNT_GLOBAL # no longer defined

Startup Files

".bashrc" and alias

  • when you start a new bash shell, the shell is configured via startup files
  • 3 types of shells
    • interactive -- we can execute commands
    • non login -- not a shell that was created as a result of a successful login
  • adding an alias for rm
    vim .bashrc
    # r:recursive; f:force; i:interactive
    alias del='rm -rfi'
    alias c='clear'
    # save the file

    # rerun all the commands in the .bashrc file
    source .bashrc

Redirecting Input and Output

  • 3 conceptual files are always open
    • stdin | 0
    • stdout | 1
    • stderr | 2
  • redirecting output
    • ls /etc/ > ~/dir-contents
  • append content to existing files
    • ls /etc/ >> ~/dir-contents
  • redirecting input
    • head < /etc/passwd
    • shell takes the entire content of the file and sends it to the head utility
  • redirecting error to "black hole"
    • find / -name 'sample.txt' 2> /dev/null
  • redirecting multiple at the same time
    # redirecting both output and error
    find / -name 'sample.txt' &> all.txt
    # or -- the SAME
    find / -name 'sample.txt' > all.txt 2>&1

    # redirecting to different locations
    find / -name 'sample.txt' > location.txt 2> /dev/null

Pipes

  • connect the stdout of one command to the stdin of an other command
  • examples
    # look through the directory listing
    ls -l /etc/ | less
    ls -l /etc/ | head -n 20 | tail -n 5
    find / -name 'sample.txt' | less
  • if you want to redirect the stderr too --> use "|&"
    • find / -name 'sample.txt' |& less

Command History

  • previously executed commands
    • history
  • execute previously executed command
    • !199
  • indirectly excute last command
    • !-1
    • !!
  • executing the last cat command
    • !cat
  • modifying the previously executed command
    • ^file1.txt^fileA.txt
    • text between the first set of carrots --> the previous command
    • between the next --> the replacement text
  • history size is determined by HISTSIZE and HISTFILESIZE in ".bashrc"

Command Substitution

back-ticks (`) and $(...)

  • examples | with back ticks to execute those inside first
    • once the inside is executed, it will substitute the content of the file with the command
    # use backticks `
    ls -l `cat file-list.txt`
    # or -- the SAME -- (preferred)
    ls -l $(cat file-list.txt)