This blog post describes how you can configure Windows Subsystem for Linux (WSL) to invoke scripts that set environment variables, define aliases, call commands, and generate output when you start a Bash shell.
When you start a Bash shell, the system sources the .profile file if it exists in the home directory of the current user (unless ~/.bash_login or ~/.bash_profile exists).
Sourcing using the source command or the dot operator (.) invokes the contents of a file as if the user had typed them into the shell window, without cloning the environment to a new shell interpreter process as occurs when you invoke a shell script from a shell window.
Because the file name starts with a dot, the .profile file is hidden by default from the ls command that lists directory contents. Add the -a option to the ls command to include hidden files and directories.
The ~/.profile file may be very simple. For me on Ubuntu 20.04 LTS, the default .profile sources the ~/.bashrc file if it exists and may adjust the PATH environment variable that defines the order of directories in which the system looks for commands.
Bash initialization customizations belong in .bashrc or files that it in turn sources. For me, the default .bashrc file sets some aliases, defines some variables and environment variables including PS1 (the system prompt), sources ~/.bash_aliases if it exists, and sources one of two possible bash_completion files if they exist.
I recommend minimal updates to any of these files. Instead, create one or more custom files, potentially on a OneDrive or other system shared to multiple Windows accounts, WSL accounts, and machines (Use OneDrive to Share WSL Commands Between Accounts and Machines – wslguy.net). I use my initials, JW, and I do not use a separate file for aliases or completion.
To get the default .bashrc file to source the ~/.jwshrc file and the ~/bin/.jwshrc file if either exists, I add the following code to .bashrc for each WSL account on each machine. It is more convenient to update the logic that sets PS1 environment variable that sets the prompt in the .bashrc file than a custom file and the change is relatively insignificant and benign.
#jw start #in PS1 change \D{%T} to \D{%F %T} to add date to prompt #in this order, ~/bin/.jwshrc can override ~/.jwshrc if [ -f ~/.jwshrc ]; then source ~/.jwshrc fi if [ -f ~/bin/.jwshrc ]; then source ~/bin/.jwshrc fi #jw end
It is convenient to update the PS1 environment variable that sets the prompt in the .bashrc file rather than a custom file and the change is relatively insignificant and benign.
In the .jwshrc file in the /bin folder of the OneDrive, I define aliases, functions, and environment variables, invoke commands, and write output (keyboard shortcut reminders for Windows Terminal) to the screen. This is some of it.
#To include date in prompt update PS1 in .bashrc \D{%F %T}
unalias cd > /dev/null 2> /dev/null
function wcd()
{
if [[ "$#" -ne 1 ]]; then
cd "$@"
elif [[ "$1" == "-" ]]; then
cd -
elif [[ "$1" =~ "\\" ]]; then
path=`wslpath "$1"`
cd `realpath "$path"`
else
path=`realpath "$1"`
cd "$path"
fi
}
alias cd=wcd
alias lj='ls -lrahcspFA'
alias ljt='ls -ltrahcspFA'
alias cls=clear
alias vi=sub
alias calc='calc.exe'
alias clip="clip.exe"
cd ~
WINUSER=`echo $PATH | perl -ne 'print "$1\n" if /.*\/c\/Users\/([^:\/]*)/'` ; export WINUSER
ONEDRIVE=/c/mnt/data/onedrive ; export ONEDRIVE
echo ""
echo "Ctrl+C: Cancel Command"
echo "Ctrl+V: Paste"
echo "Ctrl+D,Alt+F4 Exit"
echo "Ctrl+Z: Background Command (fg to foreground)"
echo "Ctrl+Enter Toggle fullscreen"
echo "Ctrl+, (comma) Edit settings.json"
echo "Ctrl+Shift+P Command pallete"
echo "Ctrl+Shift+F Find text"
echo "Ctrl+Shift+T New tab"
echo "Ctrl++ Increase font size"
echo ""
echo "Selecting text copies that text to the clipboard."
7 thoughts on “WSL: Configure Bash Shell Initialization”