User Tools

Site Tools


howto:bashprofile

This is an old revision of the document!


bash profile tips

what is

The ~/.bash_profile file is a shell script that is executed every time you start a new terminal session on a Unix-based operating system (such as Linux or macOS). It is typically used to set environment variables and customize your command prompt.

Here are some things you might see in a typical ~/.bash_profile file:

setting environment variables

You might use the export command to set environment variables, which are values that can be used by programs or scripts running on your system. For example, you might set the PATH variable to include directories where your programs are installed, like this:

export PATH=$PATH:/usr/local/bin

Or maybe you want to put some information in the envvars that are used inside some script, like a password or API-key:

export loginname=myName
export password=somethingSecret123
export APIkey = 12345-abcde-12345-ABCDE-00100
 
# NOTE: the above is an example, your bash_profile is NOT a safe place to keep passwords.
# more real-world examples from my own machines:
 
### set nano as default editor
export EDITOR=nano
export VISUAL="$EDITOR"
 
# colored GCC warnings and errors
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

defining aliasess

You can define aliases for common commands to make your life easier. For example, you might define an alias like this:

alias ll='ls -alt'

This creates an alias ll that runs the ls -alt command, which shows a detailed listing of files in the current directory sorted by time (newest first).

# more real-world examples from my own machines:
 
### default settings for common commands
alias ls='ls --color=auto'
alias ll='ls --color=auto -alth'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
 
### print current time in UTC
alias utctime="echo 'The current time in UTC is: ' && date -u"

customizing your prompt

You can customize your command prompt to display information like your current directory or your username. For example, you might set your prompt like this:

PS1='\u@\h:\w\$ '

This sets your prompt to display your username and hostname, followed by the current working directory and a dollar sign.

# more real-world examples from my own machines:
PS1="K: \[$(tput bold)\]\[\033[38;5;33m\]\t\[$(tput sgr0)\]:\[$(tput sgr0)\]\[\033[38;5;9m\]\w\[$(tput sgr0)\]\\$\[$(tput sgr0)\]"
 
# Here's a breakdown of what each part of the command does:
#
# PS1= : Sets the value of the PS1 environment variable to the following string.
# K: : Adds the character "K" to the beginning of the command prompt. (an indicator for myself as to which multipass VM i'm currently working in)
# [$(tput bold)] : Sets the text following this to be displayed in bold.
# [\033[38;5;33m] : Sets the text following this to be displayed in a dark shade of green.
# \t : Displays the current time.
# [$(tput sgr0)] : Resets the text display settings to their default values.
# : : Adds a colon to the command prompt.
# [$(tput sgr0)][\033[38;5;9m] : Resets the text display settings, then sets the text following this to be displayed in a light blue color.
# \w : Displays the current working directory.
# [$(tput sgr0)] : Resets the text display settings to their default values.
#
# result: K: 12:34:56:/home/user$

The formatting language used in the command prompt string is called the Bash Prompt Escape Sequences, which is a set of special codes that control how the prompt is displayed in the terminal.

You can find more information about the syntax and available codes for the Bash Prompt Escape Sequences in the Bash manual, which is available on most Unix-based systems by typing man bash in the terminal.

Functions configuration

If you spend a lot of time on the CLI you can use it to create handy functions like for example go a google web search from the command line as such:

### terminal shortcuts
function gg() { open https://sometool.com/?qs-q="$1"; }
function google() { open https://www.google.com/search?q="$1"&btnK=Google+Search; }

Calling the following command would then open a browser tab with the corresponding google-search: google "mysearch".

tuxsay

This should be installed on any computer where the terminal is used regularly, for obvious reasons (cowsay with tux layout):

### tuxsay/cowsay & fortune setup
#i="$(fortune -s computers)" && cowsay -f tux $i && say $i
fortune -s computers | cowsay -f tux

bash profile files and locations

Here is a list of the most common files and paths that can be used as the bash profile:

  • ~/.bash_profile: This is the user-specific bash profile file that is executed when a user logs in to a terminal session. If this file exists, it takes precedence over other bash profile files.
  • ~/.bash_login: If ~/.bash_profile does not exist, then ~/.bash_login is executed instead, if it exists.
  • ~/.profile: If neither ~/.bash_profile nor ~/.bash_login exist, then ~/.profile is executed as the default bash profile file.
  • /etc/profile: This is the system-wide bash profile file that is executed for all users who log in to a terminal session.
  • /etc/profile.d/*.sh: This is a directory containing shell scripts that are executed when a user logs in to a terminal session. Each script in the directory should end with a .sh extension, and should contain valid shell commands.

but what about: bashrc?

~/.bashrc is a file that is sourced by bash when an interactive shell is started. Unlike the bash profile files (~/.bash_profile, ~/.bash_login, and ~/.profile), ~/.bashrc is not executed when you log in to a shell session, but rather each time a new interactive shell is started.

terminal session VS interactive shell

A terminal session and an interactive shell are related concepts, but they refer to slightly different things.

A terminal session refers to the process of connecting to a shell through a terminal emulator, either locally or remotely. When you open a terminal window or console, you are starting a new terminal session. A terminal session provides a way for a user to interact with the shell, typically by typing commands at a prompt and receiving output in response.

An interactive shell, on the other hand, is a shell that is running in interactive mode, meaning that it is designed to provide an interface for the user to interact with it directly. When you start a new shell by running the bash command, for example, you are starting an interactive shell. An interactive shell typically provides a prompt that the user can type commands into, and it can execute those commands in real-time.

In practice, the terms “terminal session” and “interactive shell” are often used interchangeably, since the two concepts are closely related. However, it's worth understanding the distinction between them, since it can be helpful when troubleshooting issues or customizing your shell environment.

howto/bashprofile.1676584063.txt.gz · Last modified: 2023/02/16 21:47 by kamaradski