Customizing my WSL bash prompt

Callback Insanity
7 min readJan 29, 2020

As part of my technical stories, I post a lot screenshots of the commands I run on my bash shell. This is a story about how to customize the default bash shell that ships with Windows Subsystem for Linux, Ubuntu distribution.

Photo by Marvin Heilemann on Unsplash

By default most shells include your host name, which is Personal Identifiable Information (PII) that you may not want to expose for security hygiene purposes.

I have been so busy Dockerizing stuff and in local DevOps land in general that I haven’t had the time to personalize my bash profile to remove the hostname from the shell prompt.

Well, since I have published at least a dozen articles on Medium.com, I thought I deserved a little time to take care of this. Today’s the day.

Today’s the daaaaaay !!!

Currently on Windows Subsystem for Linux (WSL), when you open a shell it sources $HOME/.profile . From there, .profile sources .bashrc . Again, this is by default when you install WSL.

Therefore I am going to be modifying my PS1 prompt variable on .profile , since I never use WSL non-interactively, and if I did, I personally would not care about the contents of the prompt provided by the PS1 variable.

I think this is what I might settle with, for now:

# On /home/wsl/.profile
export PS1="\D{%Y-%m-%d-%I:%M%p} \u@$WSL_DISTRO_NAME: "

When I installed WSL, the default user name that I created is called wsl . I did this so I could easily distinguish between any of the various shells I might simultaneously have open on my Windows host.

At any time I might have a cmd.exe, WSL, and Docker container prompt open. Seeing the user name wsl clearly identifies to me which shell is the WSL one.

WSL also supports multiple distributions at the moment, and I am in fact using both WSL Ubuntu (for Docker Compose) and WSL Alpine (for running PHP on Windows). If I had a way of identifying which WSL distribution the shell belongs to in my PS1 prompt, not only I would I be able to identify that it’s a WSL shell, but also which WSL distribution the shell belongs to.

Typing env on WSL Ubuntu gives me a list of the environment variables available:

As you can see, the WSL_DISTRO_NAME environment variable tells us which WSL distribution this shell belongs to.

As for putting the user name in the prompt, the \u variable indicates the current shell user.

With that, if I do:

PS1="\u@WSL_DISTRO_NAME"

It would yield:

wsl@Ubuntu-18.04

But typically there’s a space between the shell prompt and the actual commands that you are running, so a space preceded by a colon would be useful:

PS1="\u@WSL_DISTRO_NAME: "

In addition, although it might be a bit too much, I find it super useful to know when did I run a command. You see, that way I can tell myself: Oh, you started working on this container workload 8 hours ago, at midnight!

There is a way to specify the current time in the prompt as well, using variables. While there are a number of pre-formatted date variables available as well, I wanted to use a concise date format that included both the date and the time in as little space as possible.

The \D{format} variable allows me to do just that. The formatting parameters come Linux’s own string from time strftime function.

In my case, I settle for:

\D{%Y-%m-%d-%I:%M%p}

Which combined with the rest of the prompt, yields:

2020-01-29-12:59AM wsl@Ubuntu-18.04:

While I could have gone all Airborne Ranger and use Zulu Time by specifying the %z time zone with a 24-hour format %H , I thought Ante-Meridiem/Post-Merdiem would be more civilized around here. But it’s good to know there’s always that option for those involved in more tactical applications.

Here’s the old shell leaking PII data on the left hand side, versus the sanitized shell on the right hand side:

There’s two more things missing on my new shell prompt:

Colors: Swag!

PWD: The current working directory. This is provided by the \w flag.

Here’s how to work with bash colors on Ubuntu.

I do want the prompt to be green again, because it looks cool on a black background. And the working directory to be blue, because it also looks good.

The color is specified as follows. To colorized a section of text, you use the opening tag:

\[\033[COLORm\]

Then to “close” that color tag, you use:

[\033[00m\]

Where 00m indicates to clear that color.

In my example, this would look like this:

Specifying color sections

Here’s the difference, with green added:

Green pill, or White pill, Neo

Much better, ain’t it? Now to the working directory bit.

Adding the \w flag after the colon, followed by $ dollar character to signify the end of the prompt, yields the following result:

I am not sure I need a space between the colon and the working directory, or between the working dir and the dollar sign, so I’ll remove those. But I’ll also add the blue color. The color code for blue is 34 .

Here’s the result:

How bow dah? Cash me in bash?

You know, for a shell so dapper — the date/time could sure use some pizzazz. What color to use? Oh, the first world class problems. We got options!

I tried cyan 36 on the date-time first, but thought the blue 34 on the working directory is a a bit pale on the black background. So I switched it to yellow on the date-time, then cyan on the working directory. It’s not a Picasso, but I’ll get honest work done:

It’s not much, but …

And with that, I’ll have all the information I’ll need for my adventures in containers: date-time, WSL distribution, and current working directory. Along with some swag!

Here’s the final concoction:

export PS1="\[\033[33m\]\D{%Y-%m-%d-%I:%M%p}\[\033[00m\] \[\033[32m\]\u@$WSL_DISTRO_NAME\[\033[00m\]:\[\033[36m\]\w\[\033[00m\]$ "
Itsssa beautiful!

And as you can see already from the different timestamps that I already added to the prompt, I started writing this story roughly at 1am, or 15 minutes before that:

Started at 1am

And finished roughly at 1:45am:

Meaning it took me about one hour to do the research for this article, do the actual technical implementation, and write the article with screenshots and cheesy meme-fueled puns included. Productivity!

Business! Metrics!

Remember that without the export keyword in the .profile file, the PS1 variable just stays local to .profile and it doesn’t take effect on the shell itself. Make sure to add it to the prompt variable via export PS1=... ..

2:15am: After looking at Junseo Park’s prompt I felt mine should be more color-harmonious, because well, OCD details are totally relevant, right? After all, the shell is where I spend at least 30–45% of my time, the other being actual code.

Here’s a slight modification to use cyan 36, light magenta 95, and purple 35:

Ebony and Ivory. Erm, Cyan and Magenta … In Harmony

And the final PS1 :

export PS1="\[\033[36m\]\D{%Y-%m-%d-%I:%M%p}\[\033[00m\] \[\033[95m\]\u@$WSL_DISTRO_NAME\[\033[00m\]:\[\033[35m\]\w\[\033[00m\]$ "

Also added some more links to the bibliography below, so make sure to check those out if you fancy more erm, fanciful options!

.

.

And Live From Hipster Bushwick, New Yawwwk

Happy Dockerizing !

.

.

.

Bibliography

.

.

--

--

Callback Insanity

Organic, fair-sourced DevOps and Full-Stack things. This is a BYOB Establishment — Bring Your Own hipster Beard.