Search code examples
bashmacosterminaloh-my-zsh

mac terminal computer name is garbled


When I use terminal enter zsh, my computer name is garbled, n3-85-8 instead MacBook-Pro. Sometime so as the bash. Do anyone know why? And how to fix it.enter image description here


Solution

  • There are two effects happening here:

    • Bash only reads the hostname (as displayed in the prompt) once at shell startup, which means you only see the change when you start a new shell, not when your hostname changes.
    • macOS by default changes its own hostname based on the network configuration

    You can configure your computer not to change its hostname (see for example this question). Or, you can configure bash to use the computer's persistent LocalHostName in the prompt. This value does not change when you connect to a different network.

    You can edit your ~/.bashrc (or related file) to have a line like:

    PS1=$(scutil --get LocalHostName)':\W \u\$'