The terminal prompt is the text shown when you open a terminal session, and it typically displays the username, hostname, current directory, or other useful information. On macOS, the default bash prompt displays the current working directory followed by the username and the host name.

Changing the bash prompt can help you personalize your terminal, provide quick information at a glance, or simply offer a refreshing change from the default. This can be especially helpful for developers or system administrators who spend significant time in the terminal.

The prompt is defined by the PS1 environment variable. By altering this variable, you can change the way your prompt looks. Different sequences, represented by a backslash followed by a character (e.g., \u for the username), determine what information gets displayed. Please note that newer versions of macOS use the zsh shell by default.

Steps to change the bash prompt in macOS Terminal:

  1. Launch the Terminal application on your macOS.
  2. View the current prompt setting.
    $ echo $PS1
    \s-\v\$
  3. Change the prompt by setting a new value for the PS1 variable.
    $ 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 changes without restarting the terminal.
    $ source ~/.bash_profile
  6. Verify the change by opening a new terminal session or window, and your prompt should reflect the new structure.
Discuss the article:

Comment anonymously. Login not required.