scutil
command to get the machine name from your System Preferences, not only will you get the right name, but it will also consistently reflect your Sharing settings.The syntax for getting the machine name with
scutil
:scutil --get ComputerName
So, for example, to have a prompt containing the user name, the machine name and the current directory, we could add to our startup script something like:
PS1='[\u@`scutil --get ComputerName`:\W]\$ '
Were you to ever change your machine name, this configuration would automatically reflect it on your terminal prompt.