You change the prompt on the fly by exporting PS1 into the current shell, not by editing the file and hoping. The .bashrc edit lasts; the export is what takes effect within a second. So the working move is: export a new PS1 in the terminal you have open right now, test it against real commands, and only then paste the line into .bashrc and run source ~/.bashrc so future shells inherit it.
Here's the smallest version that works on any modern default install:
That gives you alice@host:~/projects$. Nothing reloads. Nothing restarts. It's live from the moment you hit Enter, and it disappears the moment you close that terminal window. The rest of this article is about doing it precisely, reverting it cleanly when you break it, and understanding why a prompt that looks right in one terminal fails in another.
One boundary up front: this applies to interactive Bash 4.x and 5.x on any distribution. If your login shell is Zsh, Fish, or Dash, PS1 in .bashrc is the wrong lever, and the failure looks like "nothing happened" β covered later.
Why Exporting PS1 Beats Editing .bashrc for an Instant Change
The distinction is between the file and the running process. Your shell read .bashrc once, at startup. Changing the file afterward has zero effect on the process already sitting in your terminal, because Bash doesn't watch its own config for modifications. export PS1='...' writes directly into that process's environment, which is why it's instant.
Export matters more than assignment. If you type PS1='\w$ ' without export, the variable changes in your shell but doesn't propagate to child processes. That sounds academic until you run bash to start a nested shell, or launch tmux, and find your prompt reverted to the distro default. Use export and the setting inherits downward.
Startup files load in a fixed order that explains most "my prompt won't stick" complaints. Bash reads /etc/profile, then ~/.bash_profile or ~/.bash_login or ~/.profile β whichever exists first, in that priority β for login shells. Interactive non-login shells read /etc/bash.bashrc then ~/.bashrc. A desktop terminal emulator usually launches a non-login interactive shell, which is why the common advice is "put it in .bashrc."
The gotcha: many distributions ship a ~/.bash_profile that sources ~/.bashrc, but if yours doesn't, your prompt line in .bashrc is invisible to login shells β the ones you get over SSH or from a virtual console. Check with shopt -q login_shell && echo login || echo non-login before you blame your syntax.
The PS1 Escape Codes That Actually Control What You See
PS1 is a template. Backslash sequences are substituted at render time, and knowing the eight that matter covers most prompts you'll meet. The rest is decoration.
| Sequence | Renders | Notes for on-the-fly use |
|---|---|---|
| \u | Current username | Changes if you su to another account |
| \h | Hostname, short form | \H gives the fully qualified domain name |
| \w | Working directory, ~ for home | Full path, no truncation |
| \W | Basename of working directory | Shows src, not ~/projects/app/src |
| $ | $ for normal users, # for root | The safe choice over a literal $ |
| \t | Time as HH:MM:SS, 24-hour | Fixed format; no 12-hour option in PS1 |
| ! | This command's history number | Increments per command, not per prompt |
| \d | Date as "Tue Nov 12" | Useful when you screenshot terminals for logs |
Two subtleties trip people up. First, \w respects the $HOME value at render time, so if you reassign HOME mid-session, your ~ shorthand moves with it. Second, ! and # look similar but aren't: ! is the history number of the command about to run, while # is the command number within the current shell session, which resets to 1 in every new terminal.
\t is strictly 24-hour HH:MM:SS. There's no \T for 12-hour inside PS1 β for that you need \D{format} with strftime tokens, like \D{%I:%M %p}, which is the sequence most tutorials skip and the one that answers the frequent "how do I get AM/PM" question.
Apply these by quoting in single quotes so Bash doesn't expand them early. PS1="\u@\h:\w$ " with double quotes evaluates $ and \w at assignment time in some contexts, producing a frozen prompt. Single quotes defer substitution to display time. Always single-quote PS1.
Copy-Paste Prompt Recipes You Can Export Right Now
Each of these goes into a live terminal as a single line. Paste, press Enter, look at the result. If it's wrong, the next command reverts it β nothing is written to disk yet.
1: Minimal, low-noise. export PS1='\w$ ' β one directory, one sigil. Best on narrow terminal panes and tmux splits. 2: User and host, standard SSH-friendly. export PS1='\u@\h:\w$ ' β the classic. Tells you instantly if you're root on a remote box. 3: Timestamped for logging. export PS1='[\t] \u@\h:\w$ ' β renders [14:32:07] alice@host:~/code$. Good when you copy terminal output into a bug report. 4: Two-line with history number. export PS1='[!\u@\h \W]\n$ ' β the \n pushes your command to a fresh line, so long paths never squeeze your typing area. This is the recipe people ask for most after seeing it in someone's screenshot.
Export all four in sequence if you like; each overwrites the last, and the prompt updates immediately. That's the fastest way to audition options before committing.
Watch for one failure: a prompt ending in a space plus $ that looks right but wraps oddly when you type past the terminal width. That's a color-escape problem, covered in the next section β it happens even without color if you paste wrapped sequences from a blog post, because a carriage return smuggled into the string will break line counting.
If a recipe renders literal \u instead of your username, you double-quoted it. Re-paste with single quotes. If it renders nothing at all, you exported an empty string by leaving a trailing space inside the quotes β PS1=' ' is legal and gives you a blank prompt that still accepts commands.
Keeping the Change: What to Add to .bashrc and How to Reload It
Once a recipe survives ten minutes of real use, make it permanent. The safe pattern is a guard so the line only applies to interactive shells, then a conditional color block so you don't clobber the prompt on dumb terminals or inside scripts.
Open the file with nano ~/.bashrc or vim ~/.bashrc, scroll past the "If not running interactively, don't do anything" case statement near the top on Debian and Ubuntu, and add your line below it. Then reload with source ~/.bashrc (equivalently, . ~/.bashrc with a single dot). That re-executes the file in the current shell, which is the only way to make .bashrc changes apply without opening a new terminal.
For login shells, verify the chain: run grep bashrc ~/.bash_profile ~/.profile and confirm one of them sources .bashrc. If neither does, add [ -f ~/.bashrc ] && . ~/.bashrc to ~/.bash_profile. Without that, the prompt you tested works in your desktop terminal and silently fails over SSH β a version-specific difference between how display managers and sshd invoke Bash, not a bug in your PS1.
The reload has one trap worth naming. source ~/.bashrc runs the whole file, so any syntax error anywhere in it aborts the rest β including your prompt line. If your prompt doesn't change after sourcing, run bash -n ~/.bashrc to syntax-check the file without executing it. It prints the offending line number and exits silently on success.
Adding Color Without Breaking Line Wrap
Colored prompts corrupt line editing unless you bracket the escape sequences in [ and ]. Those tell Bash the enclosed bytes are zero-width, so it computes the prompt's visible length correctly. Omit them and you get the classic symptom: you type a long command, it wraps to the left margin over your own text, and backspacing behaves strangely.
The raw ANSI sequences are \e[32m for green and \e[0m to reset. A correct green user-at-host prompt:
export PS1='[\e[32m]\u@\h[\e[0m]:[\e[34m]\w[\e[0m]$ '
Every opening color gets a matching reset inside brackets. Every escape pair sits inside [ ]. Break either rule and the wrap bug returns.
The portable way uses tput so the sequences match your $TERM. Define color variables first, then reference them: GREEN=$(tput setaf 2) and RESET=$(tput sgr0), then build PS1 from [$GREEN]. This survives on terminals that don't speak the same ANSI dialect, which mostly matters on old serial consoles and some embedded devices.
Version note: Bash 4.4 and later handle [ ] more predictably around multibyte prompt content than 4.3 did. If you're on a distro still shipping 4.3, colored prompts containing non-ASCII characters may miscount width regardless of correct bracketing β confirm your version with echo $BASH_VERSION and prefer ASCII-only decorative characters if you see wrapping.
A cheap diagnostic: echo -n "${PS1@P}" | wc -c. The @P expansion renders PS1 to its display form, and comparing that count to your terminal's column count shows whether the prompt is being measured accurately.
Reverting Instantly When a Prompt Experiment Goes Wrong
Three escapes, in order of blast radius. Smallest first.
1: Restore the default string. export PS1='\u@\h:\w$ ' β restores the standard prompt in the current shell only. The file on disk is untouched. Use this the moment a colored prompt misbehaves. 2: Re-source the file. source ~/.bashrc β rereads your saved configuration and overwrites whatever you exported manually. This is your undo after a bad permanent edit, provided the file itself is still valid. 3: Start a fresh shell. exec bash replaces the current shell process with a new one that reads .bashrc from scratch. bash alone starts a child shell and leaves you one exit deep β exec bash is cleaner and leaves no nesting.
If the prompt is so broken you can't see your cursor, remember the shell still accepts input. Type blind, or press Ctrl+C to get a clean prompt line, then stty sane followed by Enter to repair terminal state. stty sane fixes echo and line discipline problems that a mangled escape sequence can cause.
The failure case worth memorizing: a prompt in .bashrc that references a command not on PATH. If you build PS1 using $(git branch --show-current) and open a terminal before git is installed or on a machine where it's missing, the substitution errors on every single prompt β your terminal floods with error lines and becomes hard to use. Guard it: wrap the command substitution in a conditional that tests for the binary with command -v git first. That test costs microseconds and prevents an unusable terminal.
Checking a Prompt Works Before Every New Shell Sees It
Test in a throwaway shell, not your working one. bash --norc -i starts an interactive shell that skips .bashrc entirely, giving you a clean baseline to compare against. Export your candidate PS1 there, break it, revert, and close β no risk to the shell you're actually working in.
Next, test the login path separately, because that's where most permanent edits fail. bash -l -i simulates a login shell and shows whether your prompt survives the .bash_profile chain. If the prompt is correct in a terminal but default over SSH, this is the test that proves it.
Verify with bash -n ~/.bashrc before every source. Then confirm the prompt is actually what you think by running echo -n "${PS1@P}" β you'll see the rendered string as Bash will display it, including color codes, without needing to read your own screen. Pipe through cat -v to make the invisible escape bytes visible.
One more check that outranks the rest for multi-user machines: confirm \u and \h render correctly after su to another account. A prompt hardcoded with literal strings instead of escape codes will still show the original user after you switch, which is a genuine safety issue β you'll believe you're one user when you're root.
Why PS1 Changes and Then Silently Disappears
The most-reported symptom is a prompt that works until something resets it. Four causes, and each has a distinct fix.
Root causes your prompt reverts when you launch a nested tool. Some tools spawn a login shell or a shell with --norc; your non-login .bashrc prompt never loads. Fix: put the prompt line in a file both paths read, or export BASH_ENV pointing at a snippet .bashrc also sources.
The prompt resets inside sudo -i or su -. Those start login shells as the target user β usually root β and read root's profile, not yours. Your prompt isn't built to load there. That's expected behavior, not a failure.
A shell framework overwrites you. If .bashrc sources a prompt manager (some distros wire one in by default), your line placed above that source gets clobbered on every reload. Put your PS1 assignment after every framework source call in the file, or disable the framework's prompt module per its own instructions.
The whole prompt never appears in scripts or non-interactive sessions. That's correct: non-interactive Bash skips .bashrc, and prompts are meaningless there. If you want a specific PS1 in non-interactive shells for logging, export it explicitly rather than relying on .bashrc.
Calling PS1 From External Variables and Functions
Once a prompt is comfortable, people try to modularize it. Two approaches, with a clear winner for on-the-fly work.
Inline concatenation in .bashrc works but gets unreadable past four color blocks. A conditional block that builds a variable per host β green for your workstation, red for production boxes β is the most common real-world use. Test the hostname with [ "$(hostname -s)" = "prod01" ] and assign a different PS1 in each branch. This is where the "change on the fly" idea pays off most: same recipe everywhere, different rendering depending on where you land.
A function lets you reframe scope at render time. PROMPT_COMMAND is the hook β Bash runs it before displaying each prompt, and it can reassign PS1 as you go. A 12-line function can compress your home directory to ~, shorten deeply nested paths after three levels, and append a marker when you're in a git repository. The catch: PROMPT_COMMAND runs on every prompt display, so anything slow inside it β a network call, a version check β makes every Enter feel laggy. Time it by wrapping the function call with time in a loop of fifty iterations.
Verdict: for one to five recipes on a few machines, direct export PS1='...' wins every time. It's one line, it reverts with one line, and it has no failure modes you can't see. Reach for a function only when the prompt genuinely needs to change based on runtime state that varies per directory.
FAQ
Why does my prompt change disappear when I open a new terminal?
Because export PS1=... modifies only the running shell process. A new terminal starts a fresh Bash that reads .bashrc from disk, and if your line isn't saved there, it reverts to defaults. Save the line inside .bashrc below any interactive-check guard, then run source ~/.bashrc. If the new terminal still ignores it, check whether it's a login shell β in that case .bash_profile must source .bashrc for the line to load.
Do I need export in front of PS1 in .bashrc?
No, inside .bashrc a plain assignment is enough because that file is read by the shell that will use the variable. The export keyword is what you need at the command line, where you're writing into a live shell and want the value to inherit into child shells and tools like tmux. Mixing them up produces the exact symptom where a nested bash shows the default prompt while the parent shows yours.
Can I get a 12-hour clock in a Bash prompt?
Yes, through \D rather than \t. The sequence \D{%I:%M %p} renders something like 02:32 PM, whereas \t is locked to 24-hour HH:MM:SS and has no format option. The braces hand Bash's strftime implementation your format, so any strftime token works β \D{%Y-%m-%d} gives an ISO date. Single-quote the whole PS1 so the braces reach Bash unexpanded.
How do I make the prompt different on root versus a normal user?
Branch on $EUID, which is 0 for root and the user's numeric ID otherwise. A single conditional like [ "$EUID" -eq 0 ] && export PS1='[\e[31m]\u@\h:\w#[\e[0m] ' gives root a red prompt while normal users keep the standard one. This beats relying on $, which only changes the sigil character and can be easy to miss in a busy terminal. Many distributions already ship a similar branch in the default .bashrc.
What's the fastest way to test a prompt without editing my config?
Run bash --norc -i to get an interactive shell with none of your startup files, then export candidate prompts there. Broken formatting, bad color escapes, and empty strings are all harmless in that throwaway process. Close it with exit and your working shell never noticed. This costs under five seconds and eliminates the "I bricked my prompt" class of problem entirely.
Will a colored prompt slow down my terminal?
Not if the escape sequences are bracketed in [ ] and precomputed into variables. A correctly built colored prompt renders in microseconds β the same cost as a plain one. Trouble starts when PROMPT_COMMAND runs a command on every render, like querying git status or the current Kubernetes context. Each such call escapes to a subprocess; a slow one adds visible lag to every Enter press. Move slow lookups into a cached variable updated on cd instead.
Why does my prompt run into my long commands on the same line?
Almost always a width miscount caused by unbracketed ANSI color codes. Bash needs [ ] around every zero-width sequence to know its visible length; without it, the terminal thinks the prompt is longer than it is and wraps to the wrong column. Fix each color pair to [\e[CODE m]...[\e[0m] and retest with a command long enough to wrap. If you're on Bash 4.3 with multibyte prompt text, check the version first.
Does PS1 in .bashrc apply to scripts?
No, intentionally. Non-interactive Bash skips .bashrc and never displays a prompt, so PS1 has no effect in scripts or piped commands. If you need a variable from your prompt configuration available to scripts, export it explicitly in .bashrc outside the interactive guard, and scripts that source .bashrc will inherit it. Relying on PS1 itself in a script does nothing β it's a display template, not a general-purpose variable.
The prompt is one of the cheapest pieces of your environment to experiment with, provided you work the right way around: export first, verify in a throwaway shell, and save to .bashrc only after the recipe holds up across a login shell. That order means a mis-typed color code costs you one export rather than a terminal session.