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.
$ echo $PS1 \s-\v\$
$ export PS1="[\@] \u@\h\$ " [09:20 AM] shakir@host$
|\h||Hostname up to the first .|
|\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|
|\s||Name of the shell (e.g., bash)|
|\v||Version of the shell|
$ 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.
$ source ~/.bash_profile
Comment anonymously. Login not required.