Profile/.profile/.bash_rc/et al.

Profile/.profile/.bash_rc/et al.

Post by Sean M Hollingswort » Thu, 23 Apr 1998 04:00:00



Could someoe please explain when, say, .profile is read as oppossed to
.bash_rc or .bash_profile, especially in X while running XDM?  I'm
having a bit of confusion about where some environment variables should
be set.

Also, what is the difference between login, interactive and
non-interactive shells?

 
 
 

Profile/.profile/.bash_rc/et al.

Post by J.M. Pad » Thu, 23 Apr 1998 04:00:00


Sean M Hollingsworth <cthu...@spiritone.com> wrote:

>Could someoe please explain when, say, .profile is read as oppossed to
>.bash_rc or .bash_profile, especially in X while running XDM?  I'm
>having a bit of confusion about where some environment variables should
>be set.
>Also, what is the difference between login, interactive and
>non-interactive shells?

I have just been trying to get a grip on these configuration files.
The following is my understanding of their usage.  I would welcome any
corrections.

1.  What files are directly associated with bash?

    A. For "login" shells:

        1.  /etc/profile

        This is always checked first.  It contains systemwide settings
        of environmental variables and programs that are to be started
        on login.  The bash shell reads this file whenever a user logs into
        the system.  A few of the variables that are set in this file are:
                HOME=  which identifies the user's home directory.
                PATH=  which lists the directories which are searched by bash
                         each time a user issues a command.  It also specifies
                         the order in which these directories are to be searched
                         for the binary associated with the command.
                PRINTER=  identfies the printer(s) available to the users.
                EDITOR=   identifies the default editor that is available.

        Individual users can either accept all of these default variables or
        personalize one or more of them by inserting different variable
        definitions in a file located in their home directory.  This
        personalized file can have one of three names:  
                ~/.bash_profile
                ~/.bash_login
                ~/.profile
        The bash shell will search for such a personalized configuration in
        the order in which they have been listed above.

        2. ~/.bash_profile

        If it exists, this is read and its commands executed.  The next
        two files are ignored.  This is where an individual user places
        his own settings of environmental variables and any programs
that
        will be started when that user logs in.  This file is checked only
        one time - at startup.  If a user switches to another shell and then
        back to bash, bash will not check ~/.bash_profile again, but will
        check ~/.bashrc instead  (see Para B1 below).

        The following are some of the things that will typically be found in
        a ~/.bash_profile file (or in ~/.bash_login or ~/.profile):

          PATH="$PATH:/home/username/bin"
          export PATH

          This would permit the bash search for executables to include
          those that are placed in a bin directory in the user's home
          directory.

          MAIL="/var/spool/mail/username"
          export MAIL

          This would identify where your mail is stored in your mailbox.

          PS1="\h: "
          export PS1

          This would change the prompt line of the user to the hostname of
          the machine.

          You can also define the ENV or BASH_ENV variable in
          ~/.bash_profile.
          See the section concerning non-interactive, non-login shells below
          for the purpose of such an entry.

        3. ~/bash_login

        If ~/.bash_profile does not exist, bash_login commands are executed
        and     ~/.profile is ignored.

        4.  ~/.profile

        If neither ~/.bash_profile nor ~/bash_login  exist, then ~/.profile
         is checked for any environmental variables and programs which are
         to be started. Settings in this file will take precedence over the
         systemwide settings in /etc/profile.

        5. ~/.bash_logout

        This runs when the user logs out and executes any commands that
        it contains.

    B.For "non-login interactive" shells:

        Any shell that was started without logging on is known as a
        non-login shell. An example is a shell started in an xterm window
        with the command:

                bash

        1. ~/.bashrc

        This file is checked when any new interactive shell is started after
        login. /etc/profile is checked only once at startup of the
session.
        When you open new shells, bash checks with ~/.bashrc    instead.
        This would be the file that a user would use to place personal
        aliases and functions.

        An alias can give a new or more precise definition to a common
        command. For example:
                ls  is the normal command used to list directories.  You could
        redifine what the ls command actually does by putting the following
        line in the ~/.bashrc file:
                alias ls="ls -lFa"

        Now when the user gives the ls command, it performs the same function
        of the lengthier ls -lFa command.

        Another frequent alias is one that causes the delete command rm to
        seek confirmation from the user before the action is actually
        performed.

                alias rm="rm -i"

        In addition to aliases a user can place personalized functions in the
        ~/.bashrc file.  One that I use is to permit printout of *.c files
        with each line numbered, including blank lines:
        #Function usage:  cnl <filename>
                cnl ()
                  { for args
                    do
                    nl -ba $args > ~/tmp/junkfile
                    done
                lpr ~/tmp/junkfile
              }

        It should be noted that the ~/.bashrc file will not be run by bash in
        a non-interactive, non-login session.  This situation occurs when a
        bash shell is opened to run a script command.  The user has no
        interaction with the shell--it is just performing a bash command.

        These non-interactive, non-login shells will execute any command
        defined by the variables BASH_ENV or ENV.  If you want to control
        some aspects of non-interactive, non-login shells, you can place the
        following in your ~/.bash_profile:

                ENV=~/.bashrc
                if [-f ~/.bashrc]; then
                  source ~/.bashrc;
            fi

        NOTE: The following is how one ISP handles the startup files:

        "Since most users want to have largely the same environment no
        matter what type of interactive shell they wind up with, whether or
        not it's a login shell, we'll start our configuration by putting a
        very simple command into your .bash_profile: ``source ~/.bashrc''.
        The source command tells the shell to interpret the argument
        as a shell script. What it means for us is that everytime
        bash_profile is run, .bashrc is also run.

        Now, we'll just add commands to our .bashrc. If you ever want a
        command to only be run when you login, add it to your
        .bash_profile."

C.For "non-interactive" shells:

        This refers to shells that are opened to run a shell script.

        1. BASH_ENV
        bash looks for this variable and expands it, sourcing the file
        which is named as the value of the BASH_ENV variable.  It then  uses
        that file for the settings.

        Since if bash is not started in POSIX mode, it will look for
        BASH_ENV before looking at ENV, it is a good idea to include the
        following lines in ~/.bash_profile:

                if      
                        [ -f `~/.bashrc']; then
                        source `~/.bashrc';
                fi

    D.  Two files that require more research:

        1.  ~/.inputrc

        My understanding is that this file can be used as a user's
        readline initialization file.  You could possibly have your vi
        command open a document in the insert rather than command mode by
        putting the commands in .inputrc.  This is also the file where you
        can put personalized key bindings.  Any application that starts up
        will check this file for bindings.

        The following is an example of an .inputrc file:

        "\e[1~": beginning-of-line
        "\e[3~": delete-char
        "\e[4~": end-of-line

        $if term=linux
        "\C-?": backward-delete-char
        $else
        "\C-?": delete-char
        $endif

        set bell-style  none
        set meta-flag           on
        set convert-meta        off
        set output-meta         on

        2.  /etc/bashrc

        This is a standard file in Linux and it is where you would put
        systemwide aliases and functions.  It would be used unless overridden
        by settings in a ~/.bashrc file.  The bash man pages do not mention
        this file at all.  You may need to put the same lines in
        /etc/profile that are suggested in .bash_profile to force the
        sourcing of /etc/bashrc.

  E.  How do you tell if a shell is interactive or non-interactive?
        You can add the following to the startup script:

        if [-z "$PS1"]; then
          echo This shell is not interactive
        else
          echo This shell is interactive
        fi

        The above means that if $PS1 does not exist, the shell is not
        interactive.  

        To insure that this is seen by bash, it should be put in /etc/profile
        and in ~/.bashrc.

Regards,
jmpa...@mnsinc.com
 "F. Nietzche will be a bore"
    -Plato