Wherever you are using macOS or Linux, you have probably used the terminal app for one reason or another.

macOS and most distributions of Linux (including Ubuntu and Fedora) use Bash as their default shell. Even Windows 10 has access to Bash now, thanks to Ubuntu on Windows 10. First released in 1989, Bash is the piece of software that makes your Terminal useful, providing not just the essentials like launching command line tools, but also providing features like auto-completion, I/O redirection, scripting, and even multitasking.

One of the features it provides is the ability to change your default shell prompt, which is stored in the PS1 environment variable. Changing it is very easy. Just type this command in your terminal:

PS1="Hello, ModMy! "

Hello, ModMy! prompt

When you do that, you will notice that your shell prompt has been replaced by the phrase "Hello, ModMy!". You can build upon this using various variables that Bash provides. Some of these variables include:

  • \d – Current date, in Weekday Month Day format (for example, Wed Nov 15)
  • \t – Current time, in 24-hour HH:MM:SS format
  • \A – Current time, in 24-hour HH:MM format
  • \T – Current time, in 12-hour HH:MM:SS format
  • \@ – Current time, in 12-hour HH:MM AM/PM format
  • \u – Your username
  • \H – Your computer's hostname
  • \h – Your computer's hostname up to the first dot
  • \w – Complete path to the current folder
  • \W – Name of the current folder
  • \$ – Insert # for the root user, $ for anyone else
  • \\ – A backslash character
  • \n – Insert a new line

You can find a more detailed list of these prompt options in the Bash manual.

Beyond using these variables provided by Bash, you can actually include the result of any command you want by enclosing it with ` characters. For example, try typing this command:

PS1="Hello from `uname`! "

This will give you a shell prompt that looks like this:

Hello from `uname`! prompt

You can also use special escape codes to make your prompt even fancier:

  • \e[1m – Bold text
  • \e[0m – Reset all text attributes
  • \e[39m – Default foreground color
  • \e[30m – Black
  • \e[31m – Red
  • \e[32m – Green
  • \e[33m – Yellow
  • \e[34m – Blue
  • \e[35m – Magenta
  • \e[36m – Cyan
  • \e[37m – Light Gray
  • \e[90m – Dark Gray
  • \e[91m – Light Red
  • \e[92m – Light Green
  • \e[93m – Light Yellow
  • \e[94m – Light Blue
  • \e[95m – Light Magenta
  • \e[96m – Light Cyan
  • \e[97m – White

A more detailed list that includes more of these escape codes is available at this website.

Using all of these tools, you can take your prompt look really fancy, for example:

PS1="Hello from \e[1m\e[35mModMy\e[0m\e[39m on `uname`! "

This will give you a prompt that looks like this:

Hello from ModMy on Linux! prompt

With all these tools, you are ready to create a terminal prompt that will look just like you want it to.

modmy@ModMyUbuntu~ $ prompt

Finally, to make these changes persist, you need to save them either to your .bashrc file (if you are on Linux), or your .bash_profile file (if you are on macOS). To do that, you can use the following command to open the nano text editor:

nano ~/.bashrc

Just add the PS1="Your Cool Prompt\$ " command to the end of the file, and the next terminal window that you open will be using your new prompt. Then, to remove this prompt, just remove the line from the .bashrc or .bash_profile file.

What do you think? Did you give customizing your Bash prompt a shot? Share your screenshots below, or tweet us at @ModMyTweets!