Security Consulting
  Unix Administration     Firewall     Intrusion Detection     Network Security     Hacking     MORE     HOME    

Bash Shell

Links

Some nice links:
  • Bash Programming HOWTO
  • BASH Prompt HOWTO
  • Working more productively with bash 2.x

    Scripts

      set -v prints the command lines as they are executed.
      set -w prints the command lines as they are executed with shell expansion.
      shift Usually only 9 arguments are handled by the shell this shifts the tenth to the ninth and so on, in order to enlarge the argument space.
      [expression] evaluates this expression (e.g. in an if-clause)

    Environment

      $# The number of shell arguments
      ${#foo} The length of a variable
      $@ The value of each shell argument
      "$@" The value of each shell argument, without empty "" references
      $* The values of all shell arguments
      "$*" The values of all shell arguments as one argument separated by IFS
      $? Exit status
      $! Number of the current background process
      $$
      !$ This has to do with History Expansion (see man pages). This command does expand to the last argument of the previous command.
      $- Check which parameters are set.
      $((...)) arithmetic substitution e.g., $((i++))
      HISTFILE=/dev/null selfexplaining, I guess.
    If you want to show the values of these variables, in emacs mode, you use CTRL-META-E and it will expand them for you. In VI mode you use

    export

    Why do you have to export environment variables? Setting them alone is not enough. This is because when you spawn a new shell, the environment is copied to the new shell, but only the exported variables are passed along. the others aren't.
    This now implies, that you can pass variables from a parent shell to a child shell, but NOT back!

    functions

    Functions are another concept of defining shortcuts. Use:
      declare -f to see what functions are defined in the current shell
    Functions, as well as the before mentioned variables need to be exported, if they should be available for some longer time. Use:
      declare -xf {functionname} to do so.
    Function definitions can be written in an ordinary file. To execute the file, use the following command:
      . {file_with_functiondefinitions}

    What is a HERE DOCUMENT

    That's pretty easy, check the following self-explaining example:
      cat << EOI
      Das ist ein Text, der nun ausgegeben wird.
      EOI
      Das ist ein Text, der nun ausgegeben wird.

    I/O Channels

    You can open more than just the three standard-channels:
      exec 3> {filename}
    This redirects channel 3 to filename. To write to it, use:
      ls -l >3
    Remember, this will always append to the file! (Not overwrite it!).
    Closing the channel is done by:
      exec 3>
    You can also:
      exec 3< {filename}; wc <&3

    Don't forget:

      grep "Tomaten" * >/dev/null 2>&1 && echo "Tomaten gefunden"

    Conditional replacement

    An example should illustrate the usage:
      $echo $capitol
      
      $ echo ${capitol:-Roma}
      Roma
      $ echo $capitol
      
      $ echo ${capitol:=Madrid}
      Madrid
      $ echo $capitol
      Madrid
      $ echo ${capitol:+"variable set"}
      variable set
      $ unset capitol
      $echo ${capitol:+"variable set"}
      
      $ echo ${capitol:?"missing capitol"}
      capitol: missing capitol
      $ capitol=paris
      $ echo ${capitol:?"missing capitol"}
      paris
      $
      

    Eval and Variables

    If you do something like: a=hello; $b='$a'; $c='$b' you can then do a: cat eval eval $c. I think you get it...

    Traps

    One can control almost all UNIX signals, except for signal 9 (SIGKILL).
    Here are some usages:
      trap '' signalnumber ignores signal.

    Advanced things

      for file in $(locate .html| grep HOWTO); do echo mv $file ${file%xhtml}; done
    What this is doing should be clear. The only cryptic thing might be the ${file%xhtml}html This cuts xhtml from the end of $file and adds html to the end. For more of these, check the manpages of bash under: Parameter Expansion.


    CopyLeft (l) 2003 by Raffael Marty