Chapter 6: Configuring Your Working Environment

6.1 Profile, login, and resource files

When Bash starts up, or the user logs into UNIX, Bash looks in the etc/ folder for the .profile file. In profile there should be a list user config files.

UNIX looks for the user config files in the locations specified in the order they are listed. When it finds one of these config files, it reads that file and then ignors any User config files that follow it… (so, it uses the first one it finds.)

The etc/.profile file is best left to MacOS to manage. It contains system wide configuration.

There are several places where you can write configuration settings that are specific to your User account without interfering with the MacOS system wide settings.

In the User folder you may find one of several options:

  1. .bash_profile
  2. .bash_login
  3. .profile
  4. .login

You will probably find a .bash_profile file in your User folder. You can also create any of the other files in this list and use them to specify configuration for your user account.

Sidenote: In bash configuration files "comments" are preceded by a pound sign:

# this is a comment

6.2 Where to put custom configuration

Lets focus on the configuration files in the User folder.

.profile and .login are generic config files and should run when logging into other shells. We are concerned with logging into the Bash shell which is the default shell used in terminal on the MacOS.

But there are other shells you may use in terminal. If your logging into another shell its best to use their dedicated config files.

We are logging into the bash shell so you could dump all your custom config into .bash_profile which will load everytime you log into bash (or bash_login.)

But if you are logged into bash and then execute bash to run a new bash sub-shell .bash_profile won't run. It only runs when you login. When opening a sub-shell the config file that runs is .bashrc.

So to make config settings you write run both when you open bash as well as when you open a bash sub-shell, create a .bashrc file and then in .bash_profile call the .bashrc file. Now dump all your commands into .bashrc.

To see how this works put this into .bash_profile

echo ""
echo -n "Welcome to Unix on Mac OS X, "; whoami
echo ""
echo -n "Today is "; date "+%m-%d-%Y %H:%M:%S"
echo ""
cal

This only runs on user login

add:

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

This loads in the configuration found in .bashrc if a .bashrc file is found.

Lastly, add this into .bashrc

echo -n "Uptime: "; uptime
echo ""

This configuration runs both when you login to bash and when you are logged in and open a new bash sub-shell.

Put all your personal user configuration here in .bashrc.

6.3 Setting command aliases

Use the alias command to specify an alias for a command

alias

lists all aliases currently configured

To create an alias:

alias ll='ls -la'

You can alias any commands or set of commands.

To get rid of an alias use unalias

unalias ll

Creating an alias by typing it into terminal only lasts for the open terminal session.

6.4 Setting and exporting environment variables

Environment variables are also know as shell variables. They are set to help configure your working environment.

$SHELL

This varible returns the default login shell for the current user.

The $ indicates to UNIX that we want it to return the value located in the SHELL variable.

To set a variable for the current session simply assign a name to a value

MYNAME='Name'

However, MYNAME won't be available to the child processes that bash starts for us. It will only be available in bash itself. To indicate to bash it should also pass along these variables or export them to other commands, programs, and scripts, we need to use the export command.

6.5 Setting the PATH variable

The PATH variable is a list of paths, separated by collens, each of which leads to a folder containing one or more UNIX executables. To instruct UNIX to check a PATH for executables, add a path you want UNIX to check to the list of existing paths. It will check the locations in the order you list them.

export PATH='/Users/smerth/.phantomjs/bin:/Users/smerth/.platformsh/bin:/Users/smerth/.nvm/versions/node/v8.9.1/bin:/usr/local/opt/git/bin:/usr/local/opt/sqlite/bin:/usr/local/Cellar/mysql/5.7.16/bin:/usr/local/Cellar/ctags/5.8_1/bin:/Users/smerth/.composer/vendor/bin:/usr/local/opt/php70/bin:/Users/smerth/.rbenv/shims:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/smerth/pear/bin:./node_modules/.bin:/Users/smerth/Library/Android/sdk/tools:/Users/smerth/Library/Android/sdk/platform_tools'

6.6 Configuring history with variables

#################
# Setting global variables 
# for the History program
#################
 
# eg. setting variables for the program "history"
export HISTSIZE=10000                      # 500 is default
export HISTFILESIZE=1000000
export HISTTIMEFORMAT='%b %d %I:%M %p '    # using strftime format
export HISTCONTROL=ignoreboth              # ignoredups:ignorespace
export HISTIGNORE="history:pwd:exit:df:ls:ls -la:ll"

6.7 Customizing the command prompt

################
# customize the bash prompt
################
export PS1="\\[\033[38;5;95m\](\t)\[\033[;38;5;190m\] \u\[\033[1;36m\]@\[\033[;38;5;203m\]macbook: \[\033[;38;5;10m\]\w\[\033[;38;5;39m\]\n👻  "
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced