Bash Profile
Bash is the shell, or CLI (Command Language Interpreter) for the Linux operating system. A shell defines the environment in which the user can execute programs, and accepts commands from the keyboard and executes them.
User Profiles
When you work with the command line frequently you will want to customize the environment. This can mean changing environment variables, such as where the shell looks for commands or how the prompt looks, or adding customized commands. Bash uses the user profile files .bash_profile
and .bashrc
to allow specific environment configurations to be defined for a user.
Profiles in MacOS
The usual convention in Linux is that the .bash_profile
file is executed for login shells (username/password prompts, ssh to remote hosts, etc.) and the .bashrc
file is used for non-login shells like Terminal. The basic idea being that the .bash_profile
should only be run once at login, and the .bashrc
for every new interactive shell. However, Mac does not follow this convention, but some applications which run on Mac do. The solution is to put all configuration code in .bashrc
and include (source) .bashrc
from within the .bash_profile
file. More info below.
Another catch. With the release of Catalina, Apple has changed the default shell for macOS to zsh
due to licensing concerns. It works the same but uses differently named configuration files in the same location. For instance .bashrc
is now .zshrc
and .bash_profile
is .zprofile
.
Profile Files
- In Terminal change to your current users home directory:
cd ~/
- List all files and directories, including hidden files with:
ls -la
- Create the
.bash_profile
file:
touch .bash_profile
- Use
ls -la
to list the files again to confirm the new.bash_profile
file was created. - Open the
.bash_profile
file in a text editor (nano .bash_profile
). - Add the following code to include the
.bashrc
file into the.bash_profile
file:
[ -r ~/.bashrc ] && . ~/.bashrc
- Save and close the file.
- Run
cat .bash_profile
to print so you can verify the contents of the file. - Create the
.bashrc
file:
touch .bashrc
Now all new additions to the user profile can be added to the .bashrc
file.
Profile Configurations
- Open the
.bashrc
file in a text editor (nano ~/.bashrc
). - Add configurations and then save and close the file.
- Changes will appear the next time a new Terminal window is open.
Command Prompt
# Command prompt.
export PS1='\u@\h:\w$ '
$PATH
The PATH
environment variable is a string of colon-separated (:
) values. Each value is a path to a directory. The OS will check each directory, in the order they were defined, while searching for the executable file for a program.
MacOS sets the PATH
environment variable to /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
by default.
- If you type
echo $PATH
into Terminal, and/usr/local/bin
or~/usr/bin
is first, you are ready to go. ~/usr/bin
is equivalent to/usr/local/bin
.
Eaxample
Add the folders my_directory
and my_other_directory
, from under the home directory to the PATH variable:
export PATH="$PATH:~/my_directory:~/my_other_directory"
Default text editor
Define the default console text editor in the EDITOR
variable.
export EDITOR="/usr/bin/nano"
ls Alias
Alias the ls -la
command for listing all directory content including hidden files.
alias l='ls -la'
Directory shortcut
Change to the Projects
directory when you type cdp
.
alias cdp='cd ~/Projects'