The terminal prompt in macOS displays information like the username, hostname, and current directory. This is typically set by the default bash prompt, which many users find useful but may want to customize for a more personalized or informative terminal experience.

Changing the bash prompt involves modifying the PS1 environment variable. This variable allows you to display different pieces of information in the prompt, such as the current time, shell name, or any custom text. Customizing your terminal prompt can be especially helpful for developers or system administrators who rely on quick access to certain information.

Latest macOS now using zsh as the default shell, though many users still work with bash. Adjusting the bash prompt is a straightforward process that enhances productivity by making the terminal interface more aligned with your preferences.

Steps to change the bash prompt in macOS Terminal:

  1. Launch the Terminal application on your macOS.
  2. Check the current prompt by displaying the value of the PS1 variable.
    $ echo $PS1
    \s-\v\$
  3. Modify the PS1 variable to change the prompt format.
    $ export PS1="[\@] \u@\h\$ "
    [09:20 AM] shakir@host$ 
    Character Explanation
    \u Current username
    \h Hostname up to the first .
    \H Full hostname
    \w Current working directory
    \W Basename of the current working directory
    \! History number of the current command
    \# Command number of the current command
    \$ If the UID is 0, a #, otherwise a $
    \d Date in “Weekday Month Date” format (e.g., “Tue May 26”)
    \t Current time in 24-hour HH:MM:SS format
    \T Current time in 12-hour HH:MM:SS format
    \@ Current time in 12-hour am/pm format
    \n Newline
    \s Name of the shell (e.g., bash)
    \v Version of the shell
  4. Add the export command to your ~/.bash_profile or ~/.bashrc file to make it permanent.
    $ echo 'export PS1="[\@] \u@\h\$ "' >> ~/.bash_profile

    Manually edit the PS1 declaration in ~/.bash_profile or ~/.bashrc if it already exist and for greater control.

  5. Apply the new settings immediately without restarting the terminal.
    $ source ~/.bash_profile
  6. Open a new terminal window to confirm that the prompt has been updated.
Discuss the article:

Comment anonymously. Login not required.