Sunday 9 December 2018

Key Concepts on BASH

Key Concepts on BASH
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

https://superuser.com/questions/789448/choosing-between-bashrc-profile-bash-profile-etc
For Bash, they work as follows. Read down the appropriate column. Executes A, then B, then C, etc. The B1, B2, B3 means it executes only the first of those files found.
+----------------+-----------+-----------+------+
|                |Interactive|Interactive|Script|
|                |login      |non-login  |      |
+----------------+-----------+-----------+------+
|/etc/profile    |   A       |           |      |
+----------------+-----------+-----------+------+
|/etc/bash.bashrc|           |    A      |      |
+----------------+-----------+-----------+------+
|~/.bashrc       |           |    B      |      |
+----------------+-----------+-----------+------+
|~/.bash_profile |   B1      |           |      |
+----------------+-----------+-----------+------+
|~/.bash_login   |   B2      |           |      |
+----------------+-----------+-----------+------+
|~/.profile      |   B3      |           |      |
+----------------+-----------+-----------+------+
|BASH_ENV        |           |           |  A   |
+----------------+-----------+-----------+------+
|                |           |           |      |
+----------------+-----------+-----------+------+
|                |           |           |      |
+----------------+-----------+-----------+------+
|~/.bash_logout  |    C      |           |      |
+----------------+-----------+-----------+------+


You can just have .bash_profile instead of .profile, no point of having two 

The requirements:
  • ~/.profile must be compatible with any /bin/sh – this includes bash, dash, ksh, whatever else a distro might choose to use.
  • Environment variables must be put in a file that is read by both console logins (i.e. a 'login' shell) and graphical logins (i.e. display managers like GDM, LightDM, or LXDM).
  • There is very little point in having both ~/.profile and ~/.bash_profile. If the latter is missing, bash will happily use the former, and any bash-specific lines can be guarded with a check for $BASH or $BASH_VERSION.
  • The separation between *profile and *rc is that the former is used for 'login' shells, and the latter every time you open a terminal window. However, bash in 'login' mode doesn't source ~/.bashrc, therefore ~/.profile needs to do it manually.
The simplest configuration would be:
  • Have a ~/.profile that sets all environment variables (except bash-specific ones), perhaps prints a line or two, then sources ~/.bashrc if being run by bash, sticking to sh-compatible syntax otherwise.
    export TZ="Europe/Paris"
    export EDITOR="vim"
    if [ "$BASH" ]; then
        . ~/.bashrc
    fi
    uptime
    
  • Have a ~/.bashrc that performs any shell-specific setup, guarded with a check for interactive mode to avoid breaking things like sftp on Debian (where bash is compiled with the option to load ~/.bashrc even for non-interactive shells):
    [[ $- == *i* ]] || return 0
    
    PS1='\h \w \$ '
    
    start() { sudo service "$1" start; }



No comments:

Post a Comment