Top

A Linux User Reference

Search tips
  • search ignores words that are less than 4 characters in length
  • searches are case insensitve
  • if a search does not return anything try it in Boolean mode then Query expansion mode by checking the appropriate radio button e.g. searching for 'cron' in just the Administration category returns nothing - presumably because the 50% threshold is reached. Boolean mode ignores this threshold so a search for 'cron' returns several hits
  • in Boolean mode preceding a word with a '+' means the result must include that word, a '-' means it must not
  • in Boolean mode '+crontab -anacron' means match articles about crontab that DO NOT mention anacron
  • to match a phrase e.g. 'manage system' check the Boolean mode radio button and enclose the phrase in quotes "some phrase ..."
  • in Query expansion mode the search context is expanded beyond the keywords you entered - relevancy of hits may well be degraded

THE SHELL

Bash

  • A shell is
    • a command interpreter
    • a display portal to the system
    • an operating environment
    • a facility to launch and manage programs
    • a programming language

    • Shells are distinct from the kernel and run as user programs.

    • Shells can be customised by tuning the shell's operating environment.
    • Shells are run for both interactive (users) and non-interactive (system).
    • A shell can be run from another shell.
    • Shells use a set of configuration files to establish their operating environment.
    • Shells pass on their environment to child processes.
    • Functions and variables can have the same name.
  • The shell environment

    The shell environment is initially set up via the profile files. Common environment variables initialised include:

    Variable Description
    PS1 Prompt string
    USER User Name
    UID User ID
    HOME Home directory
    SHELL Current shell
    PWD Current working directory
    HISTSIZE Number of lines to store
    $ Process ID
    PPID Parent process ID
    ? Exit code of previous command
    - Currently set bash options
    IFS Internal Filed Separator
    PATH Environment path

    Display some currently set variables

    $ echo -e $USER $UID ~ $HOME $SHELL $PWD $HISTSIZE $$ $PPID $?'\n'$- $IFS $PATH
    mark 1000 /home/mark /home/mark /bin/bash / 500 6618 6615 0
    himBH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    

    Print current environment

    $ env
    GPG_AGENT_INFO=/tmp/seahorse-mThiXG/S.gpg-agent:6274:1
    SHELL=/bin/bash
    DESKTOP_STARTUP_ID=
    TERM=xterm
    .....
    
  • Bash shell command interpreter
    /bin/bash

    GNU Bourne-Again Shell. Read the man page for details on all options.

    bash [options] [file]
    
    $ bash --help
    GNU bash, version 3.2.48(1)-release-(x86_64-pc-linux-gnu)
    Usage:  bash [GNU long option] [option] ...
        bash [GNU long option] [option] script-file ...
    GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
    .....
    
    Shell options:
        -irsD or -c command or -O shopt_option      (invocation only)
        -abefhkmnptuvxBCHP or -o option
    
    Type `bash -c "help set"' for more information about shell options.
    Type `bash -c help' for more information about shell builtin commands.
    

    Common bash shell options

    • Shell variables store environment settings
    • Shell options control the behaviour of a shell
     -     turns an option ON
     +     turns an option OFF
    
     o     <option-name>            Set option using an 'option-name'
     C     noclobber                Do not allow redirection to overwrite files
     B     braceexpand              Expand {}, default
     b     notify                   Report status of terminating background jobs
     H     histexpand               Allow !history substitution, default
     h     hashall                  Enable hashing of commands
     m     monitor                  Enable job control
     t     onecmd                   Exit after executing one command
     u     nounset                  Report empty variables as an error
     v     verbose                  Print shell input lines before running
     x     xtrace                   Trace
    

    Display currently set shell options

    $ echo $-
    himBH
    

    'i' - when present indicates that the shell is interactive.

  • Set environment variables in bash
    set
    • Bash builtin command.
    • Set a shell variable and or shell option.
    • When used without any options set displays all current environment variables as name=value pairs.
    • Also includes defined shell functions. These can be reused as input for setting/resetting.
    set [--abefhkmnptuvxBCHP] 
        [-o option] [arg ...]
    
    Some options:
     -b                        Report status of terminated background jobs immediately
     -e                        Exit immediately if a simple command exits with a 
                               non-zero status. A trap on ERR, if set, is executed 
                               before the shell exits.
     -f                        Disable pathname expansion.
     -h                        Remember the location of commands as they are looked up
                               for execution.
     -m                        Monitor mode.  Job control is enabled.
     -n                        Read commands but do not execute them.  Debug scripts.
     -o <option-name>          See common shell options below.
     -p                        Turn on privileged mode. $ENV and $BASH_ENV files are 
                               not processed
     -t                        Exit after reading and executing one command.
     -u                        Treat unset variables as an error when performing 
                               parameter expansion.
     -v                        Print shell input lines as they are read.
     -x                        Display the expanded value of PS4, the command and it's
                               expanded arguments or associated word list.
     -B                        The shell performs brace expansion.
    

    Setting shell options with set

    $ export BERT                                      (Export an unset variable)
    $ echo $BERT                                       (Returns empty string - '\n')
    $ set -u                                           (Turn on nounset)
    $ echo $BERT
    bash: BERT: unbound variable                       (Error returned)
    
    $ touch file1 file2                                (Create two files)
    $ cat file1 > file2                                (Overwrite file2 with file1)
    $ set -o noclobber                                 (Turn on noclobber)
    $ cat file1 > file2
    bash: file2: cannot overwrite existing file        (Disallowed)
    
    $ cat file1 >| file2                               (Bypass noclobber)
    $ set +C                                           (Turn off noclobber)
    $ set -x                                           (Turn on tracing)
    ++ echo -ne '\033]0;mark@dnssvr: ~\007'
      cat file1 > file2
    + cat file1
    ++ echo -ne '\033]0;mark@dnssvr: ~\007'
    
    $ echo -e '\tfred\n\t\tbert'                       (Interpret metacharacters)
       fred
          bert
    

    Setting environment variables in bash

    $ echo $SHELL
    /bin/bash
    
    $ var1=jane
    $ export VAR2=anne
    $ echo $var1 $VAR2
    jane anne
    
  • Unset a bash variable or option
    unset
    • Bash buitin.
    • With no options removes a variable definition if it exists otherwise a function definition if that exists.
    unset [options] name ....
    
    Common options:
     -v             Ensures that name is a variable definition
     -f             Ensures that name is a function definition
    

    Unsetting environment variables in bash

    $ echo $SHELL
    /bin/bash
    
    $ var="fred"
    $ export var
    $ echo $var
    fred
    
    $ unset var
    $ echo $var
    
  • Add a variable to the bash shell environment
    export
    • Shell builtin command.
    • 'export' and 'declare -x' allow parameters and functions to be added to and deleted from the environment.
    export [-fn] [name[=word]] ...
    export -p
    
    Options:
     -p             Print list of names exported to current shell
     -f             The name supplied is a function
     -n             Unexport the supplied name
    

    Print list of currently exported variables

    $ export -p
    declare -x COLORTERM="gnome-terminal"
    declare -x DBUS_SESSION_BUS_ADDRESS="unix:abstract= \
    /tmp/dbus-v77bISY2X8,guid=79d1f25bf3289837b66290064b598aa3"
    declare -x DESKTOP_SESSION="default"
    declare -x DISPLAY=":0.0"
    declare -x GDMSESSION="default"
    declare -x GDM_LANG="en_GB.UTF-8"
    declare -x GDM_XSERVER_LOCATION="local"
    declare -x GNOME_DESKTOP_SESSION_ID="this-is-deprecated"
    declare -x GNOME_KEYRING_PID="3493"
    .....
    

    Export variable var then unexport it

    $ echo $$
    21380
    
    $ var="fred"
    $ bash                           (Start another shell)
    $ echo $$
    21399
    
    $ echo $var                      (Variable var has not been exported)
    $ exit                           (Return to original shell)
    exit
    
    $ echo $$; export var;bash       (Export the variable and start another shell)
    21380
    
    $ echo $$; echo $var
    21415
    fred                             (Variable now accessible)
    
    $ exit
    exit
    
    $ export -n var;bash             (Unexport)
    $ echo $$; echo $var
    21431
    $ 
    
  • Bash variable
    BASH_ENV

    A script invokes the bash shell first in order to execute it's commands. If the script has not been sourced it will run in it's own non-interactive shell in which profiles are NOT read.

    If the BASH_ENV variable is set, 'bash' expands the value and assumes it is the name of a file. If the file exists, 'bash' will execute the file first.

    Two test files - test.sh, fred.sh

    #!/bin/bash
    # test.sh
    echo "test.sh"
    echo $PID $PPID
    
    #!/bin/bash
    # fred.sh
    echo "fred.sh"
    echo $PID $PPID
    

    Set BASH_ENV to ~/test.sh and run ~/fred.sh

    # export BASH_ENV="$HOME/test.sh"
    
    # echo $$ $PPID         (Determine current shell)
    2846 2821
    
    # . fred.sh             (Source script fred.sh so it runs in the current shell)
    fred.sh
    2846 2821               (Confirms current shell and $HOME/test.sh is not run)
    
    # ./fred.sh             (Do not source fred.sh - BASH_ENV is read)
    test.sh                 (test.sh is run then)
    4526 2846
    fred.sh
    4526 2846               (fred.sh runs - in it's own non-interactive shell)
    
  • Bash as a symbolic link to /bin/sh
    • It is common in Linux systems for bash to be linked to '/bin/sh' using a symbolic link.
    • When 'bash' detects that it is being run under the name 'sh', it attempts to follow the startup behaviour of the older Bourne shell while still conforming to POSIX standards.
    • When run as a login shell, bash attempts to read and execute /etc/profile and ~/.profile.
    • When run as an interactive shell using the 'sh' command, 'bash' attempts to execute the file specified by the ENV variable (as it would do when invoked in POSIX mode), the '--rcfile' option will always be ignored.
    • If 'bash' is invoked by the 'rsh' daemon, then it behaves as an interactive shell, using the ~/.bashrc file if it exists.
  • Bash profiles
    • Profiles are system-wide and user specific files that configure the shell's environment.
    • The names of the profile files used vary on the Shell being used.

    Each user id has a default shell assigned to it. When logging in, in the case of the bash shell, the shell checks for and executes the following files in the order they are listed.

    Profile file Description
    /etc/profile System wide initialisation file, executed for login shells sets default PATH, variables and runs startup programs
    /etc/bashrc,/etc/bash.bashrc System wide initialisation file. Usually contains aliases and functions
    ~/.bash_profile Personal initialisation file, executed for login shells, if exists runs this one else
    ~/.bash_login If exists runs this one else
    ~/.profile If exists runs this one
    ~/.bashrc Individual per-interactive-shell startup file, run once logged-in. Common to check for and run from ~/.bash_profile
    ~/.inputrc Individual readline initialisation file. Contains key bindings and variable that affect how bash responds to keystrokes. Default: respond like Emacs editor
    ~/.bash_logout Individual login shell clean up file, executed when a login shell exits
  • Personal initialisation file
    ~/.bash_profile

    Default entries

    unset INPUTRC
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
            . ~/.bashrc
    fi
    
    # User specific environment and startup programs
    EDITOR=vi
    PATH=$PATH:$HOME/bin
    Export PATH EDITOR
    
  • Individual per-interactive-shell startup file
    ~/.bashrc

    Do your own personalisation here ..

    # Source global definitions
    if [ -f /etc/bashrc ]; then
            . /etc/bashrc
    fi
    
    # system wide PS1 set in /etc/bashrc so need to override here and not .bash_profile
    export PS1="[\u:\w]\$ "
    
    # User specific aliases and functions
    # list hidden/. files
    lh () {
    dir=$1
    if [ -z $dir ]
    then
        dir="."
    fi
    ls -al --color=tty $dir | awk 'BEGIN {FS=" "} { print $8}' | grep ^"\."[^.]
    }
    
  • System login shells
    /etc/shells

    Available login shells list

    /usr/bin/es
    /usr/bin/ksh
    /bin/ksh
    /usr/bin/rc
    /usr/bin/tcsh
    /bin/tcsh
    /usr/bin/esh
    /bin/sh
    /usr/bin/screen
    /bin/dash
    /bin/csh
    /bin/bash
    /bin/rbash
    /bin/false
    
  • Change login shell
    /usr/bin/chsh
    • If a shell is not given on the command line, 'chsh' prompts for one, it will accept the full pathname of any executable file on the system.
    • Will issue a warning if the shell is not listed in the /etc/shells file.
    • Can be configured such that it will only accept shells listed in /etc/shells file, unless you are root.
    chsh [ -s shell ] [ -l ] [ -u ] [ -v ] [ username ]
    
    Common options:
     -s | --shell               Specify your login shell
     -l | --list-shells         Print shells listed in /etc/shells and exit
    
  • Login shell profile processing order

    Bash reads and executes commands in the following files (if they exist), in this order:

    /etc/profile
    ~/.bash_profile
    ~/.bash_login
    ~/.profile
    

    When a login shell exits, 'bash' reads and executes commands from '~/.bash_profile'. This behaviour can be overridden by using the '--noprofile' option.

    Set a variable to be used in a login-shell only

    $ echo 'export TESTVAR="set in .bash_profile"' >> .bash_profile
    
    $ bash                    (Invoke a new shell)
    $ echo $TESTVAR
                              (Variable not set as this shell is a non-login shell)
    $ exit
    exit
    
    $ bash --login            (Behave as a login shell)
    $ echo $TESTVAR
    set in .bash_profile      (A login shell reads ~/.bash_profile if it exists)
    
    $ exit
    logout                    (Logs out since this was a login shell)
    
    $ bash --noprofile        (Do not run any commands in profile files)
    $ echo $TESTVAR
    
    $ exit
    exit                      (No logout, not a login shell)
    
  • Non-login shell profile processing order

    Bash reads and executes commands from the following files (if they exist), in this order:

    /etc/bash.bashrc
    ~/.bashrc
    

    Usage examples

    $ bash --norc
    

    Does not run commands in '/etc/bash.bashrc' and ~/.bashrc files'.

    Run commands from an alternative file

    $ bash --rcfile <file-name>
    

    Read and execute commands from <file-name> NOT '/etc/bash.bashrc' or '~/.bashrc'.

  • Customising keystrokes
    ~/.inputrc
    • By default, the customisation file is ~/.inputrc.
    • It is read during bash startup if it exists.
    • Can configure a different file by setting the INPUTRC variable.
    • Many systems have a default key mapping in /etc/inputrc, use the $include directive to include these.

    If INPUTRC is set to /etc/inputrc then ~/.inputrc will not be read. To correct ..

    ~/.bash_profile      unset INPUTRC
    ~/.inputrc           $include /etc/inputrc
    

    Example keystroke customisation - ~/.inputrc

    # unset INPUTRC in .bash_profile then include here for default mappings
    $include /etc/inputrc
    set editing-mode vi
    
    # set Ctrl-l to run 'ls' with a carriage return
    control-l: "ls -al \C-m"
    
  • Shell aliases
    alias, unalias
    • Aliases created on the cmd-line are local to that shell.
    • System wide ones are usually defined in /etc/bashrc, local ones in ~/.bashrc.

    Bash builtin. Displays or creates shell variables

    alias [-p] [name[=value] ...]
    
    Common options:
     -p          No arguments or '-p', prints the list of aliases in the form 
                 alias name=value on standard output
    

    Create aliases on the cmd-line

    $ alias cp = 'cp -i'
    $ alias rm = 'rm -i'
    $ alias <name> ....              (Displays alias(s))
    $ alias                          (Display all defined aliases)
    alias ls=ls --color=auto
    

    Clear, delete an alias - unalias

    # unalias <alias>                (Clear an alias)
    # \unalias -a                    (Clear all)
    

    The '\' suppresses alias expansion, without it someone can alias alias causing problems.

    Add a 'permanent' alias - ~/.bashrc

    .....
    # my aliases
    alias ln="cd /home/markw049/www/nixref"
    

    'Permanent' as in it's created automatically each time you login. Simply remove from file to get rid of.