惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks Blog

CMTOPS.DEV

CMTOPS.DEV CMTOPS.DEV CMTOPS.DEV CMTOPS.DEV CMTOPS.DEV
CMTOPS.DEV
Timofey Chuchkanov · 2024-08-05 · via CMTOPS.DEV

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

~3 min read

Updated on

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
  • Where to Start
  • My Prompt
    • Main Code
    • Where I Place My $PS1
    • The Result
  • The End

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 framework 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 PROMPT=""
    local GIT_BRANCH=""
    local GIT_DIRTY=""

    if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
        if [ ! -z "$(command -v git)" ]; then
            local GIT_BRANCH="$(git branch --show-current)"
            local GIT_DIRTY="$(git status --porcelain)"
        fi
    fi

    if [ ! -z "${GIT_BRANCH}" ]; then
        PROMPT=" (${GIT_BRANCH})"
    fi

    if [ ! -z "${GIT_DIRTY}" ]; then
        PROMPT="$PROMPT dirty"
    fi

    echo "$PROMPT"
}


# 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) ► ' # This won't work with zsh

If you use zsh, set the PROMPT_SUBST option, and your prompt to this:

setopt PROMPT_SUBST # Otherwise, the `$(__cstm_git_ps1)` substitution below will be printed as-is

export PS1=$'[%n @ %m :: %d]\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.

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.

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!