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.
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
.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
- In Terminal change to your current users home directory:
- List all files and directories, including hidden files with:
- Create the
ls -lato list the files again to confirm the new
.bash_profilefile was created.
- Open the
.bash_profilefile in a text editor (
- Add the following code to include the
.bashrcfile into the
[ -r ~/.bashrc ] && . ~/.bashrc
- Save and close the file.
cat .bash_profileto print so you can verify the contents of the file.
- Create the
Now all new additions to the user profile can be added to the
- Open the
.bashrcfile in a text editor (
- Add configurations and then save and close the file.
- Changes will appear the next time a new Terminal window is open.
# Command prompt. export PS1='\u@\h:\w$ '
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 $PATHinto Terminal, and
~/usr/binis first, you are ready to go.
~/usr/binis equivalent to
Add the folders
my_other_directory, from under the home directory to the PATH variable:
Default text editor
Define the default console text editor in the
ls -la command for listing all directory content including hidden files.
alias l='ls -la'
Change to the
Projects directory when you type
alias cdp='cd ~/Projects'