A terminal screenshot demonstrating a custom prompt

You Don't Need a New Fancy Shell With Plugins to Get a Decent $PS1

~3 min read


Quite a lot of people who want to get a decent-looking shell prompt, install a fancy shell like Zsh, and a whole framework like oh-my-zsh with a bunch of plugins on top of it. You don't actually need all of this.

Contents

Intro

Sometimes, I notice people — especially those who are new to Linux and other Unix-like systems — who get bored of their shell’s default prompt.

Some of them get a shell like fish or Zsh with a framewok like oh-my-zsh. It’s ok to have a different taste, and use what you like — that’s the whole point of having so many variety in software, after all.

However, it’s not rare among such people to not even use other features of these shells, frameworks, and whatnot.

There’s no point in overcomplicating things just to set an environment variable controlling your prompt appearance. I’ve been there too at some point many years ago. That’s a skill issue.

Where to Start

Just as with everything else in this beautiful world of Unix-like, you start by reading documentation.

Open up your terminal, or a dedicated website with man pages or GNU info documents rendered to HTML, and go through your shell’s manual. There, search for PS1, an environment variable meant to describe how your main prompt should look like.

Here are a couple of examples with the corresponding sections:

Don’t rush to create the most badass prompt you’ve ever seen just yet.

Start small, experiment a bit. When you get comfortable, you can try to add colors and different shapes, if you want to. Personally, I prefer my prompt to be more minimalistic.

My Prompt

To give you a sense of what you can do without any special shell magic, here’s the prompt I use daily on my OpenBSD, FreeBSD, and Linux machines.

It works on both Bash and Korn Shell. I haven’t tested it with other shells yet.

Main Code

# This function is used to get the current branch of a git repo, if the $PWD is a git repo.
# It's referenced later to define the PS1 itself.
__cstm_git_ps1()
{
    local GIT_BRANCH=""

    if [ -d "$PWD/.git" ]; then
        if [ ! -z "$(command -v git)" ]; then
            local GIT_BRANCH="$(git branch --show-current)"
        fi
    fi

    if [ -z "${GIT_BRANCH}" ]; then
        echo ""
    else
        echo " (${GIT_BRANCH})"
    fi
}

# Note that single quotes (') are used here.
# If you use double quotes (") instead, command substitution will be performed during the config file load time.
export PS1='[\u @ \h :: \w]\n$(__cstm_git_ps1) ► '

Where I Place My $PS1

Bash

Just put this into your ~/.bashrc, and you’re good to go.

Don’t forget to reload your config.

$

. ~/.bashrc

Korn Shell

For this one, I prefer to store configuration in a ~/.kshrc file. This is a conventional name, and the shell doesn’t load this file by default.

So, an additional step is required.

# This piece of code in your ~/.profile ensures ksh loads its config from ~/.kshrc
if [ "$SHELL" = /bin/ksh ]; then
    export ENV=$HOME/.kshrc
fi

Information on this particular env variable is present in the man page description section.

Again, don’t forget to reload your config.

$

. ~/.profile

The Result

The resulting prompt looks like this:

[username @ resolv :: ~]
 ► uname -s
OpenBSD
[username @ resolv :: ~]

[username @ resolv :: /tmp/testrepo]
 (main) ►

The End

Here you go! Good luck hacking, and have fun customizing your environment!


Thank you for reading, and have a good rest of your day! (^ ~ ^ )

If you have any questions/suggestions, or found an error, contact me!


Found this article to be helpful? Consider supporting the author.