DEV Community

IsraelW
IsraelW

Posted on • Originally published at tech-snippets.web.app

I made my first blog post

Hey everyone! I started a blog called Mordy's Tech Snippets and today I posted my first article there, it's about customizing your bash prompt and I would love feedback! Feel free to ask anything in the comments! You can read it below, but head over to check my blog out!

How to customize your Bash Prompt Part 1

If you're a developer there's a big chance that you're familiar with the terminal. The terminal's used for:

  • Git
    • git push
    • git pull
    • git commit
  • Running code
    • python main.py
    • gcc/g++ main.cpp
  • Editing files
    • nano ~/.bashrc

You get the idea; the terminal's a big part of a developer's day, and today, I'm going to show you how to customize it.
If you want to make temporary changes, you can set the PS1 environment variable for only that session; for permanent changes, you can edit the PS1 variable in your ~/.bashrc file. My terminal currently looks like this. Terminal

My prompt has:

  • Date
  • Time
  • User@Host
  • Current directory enclosed in angle brackets
  • The git master branch name in parentheses

My background color theme is from this GitHub repo, but the prompt I customized myself.

Ok, on to the prompt

The prompt string in your ~/.bashrc will start with ${debian_chroot:+($debian_chroot)}, but we can ignore that. If you've never customized the prompt, it will look like PS1=" \u@\h:\w$" these are escape sequences that tell bash what to put in the prompt.

What the escape sequences mean:

  • \u means the logged-in user's username
  • \h is the hostname of the computer
  • \w is the current working directory

so "\u@\h:\w $" becomes username@computername:workingdir$. To add the date, time, customize the working dir, and add colors, we need to add more escape sequences.

The escape sequence for the date is \ d' the date format will beDay of week Month Day`

After using \ d', my prompt looks likeThu Jun 24 jwald@Momo-pc:~/blog$`

There are three different escape sequences for time.

  • \T
    • This escape sequence will show the time in H:M:S format with the hour in 12-hour format
  • \t
    • This escape sequence will show the time in H:M:S format with the hour in 24-hour format
  • \@
    • This escape sequence will show the time in H:M format along with AM or PM; the hour is in 12-hour format

I picked the \@ escape sequence, so now my prompt is Thu Jun 24 12:53 jwald@Momo-pc:~/blog$, and the prompt string is PS1=" \d \@ \u@\h:\w$"

My terminal looks like this now Terminal

The file path doesn't look that amazing. It seems a little cramped, so let's customize that. I'm going to add angle brackets < and > around the current directory path. Remember that \w is the working directory, so I'm going to change my prompt string to look like this PS1=" \d \@ \u@\h:<\w>$". Now it looks like this. Terminal I'm going to remove the colon, the dollar sign and add some spacing to make it look better. With the formatting added, my string is PS1= "\d \@ \u@\h <\w>" and the prompt looks much better. Terminal In the next post, I will show you how to colorize your prompt and make it look even better.

Mordy

Top comments (2)

Collapse
 
sloan profile image
Sloan the DEV Moderator

Hi there, we encourage authors to share their entire posts here on DEV, rather than mostly pointing to an external link. Doing so helps ensure that readers don’t have to jump around to too many different pages, and it helps focus the conversation right here in the comments section.

If you choose to do so, you also have the option to add a canonical URL directly to your post.

Collapse
 
mordypython profile image
IsraelW

Ok. I'll add it Thank you!