I'm creating a login message for my computer inside of /etc/bashrc
and would like to greet them to the computer with the computer's name showing.
Here's my code:
# (system stuff here)
echo "Welcome, $(whoami)."
When I use login
on any user, it gives me what I'd expect: Welcome, avgjoe
for example.
However, I'd like to make it output Welcome, avgjoe, to Tower of Nix
- my computer's name has been set (through System Preferences) to "Tower of Nix" already.
So, without hard-coding it in there (i.e. echo "Welcome, $(whoami), to Tower of Nix."
), how can I get the name of my computer?
I've already tried hostname
, but that outputs 192.168.2.13
.
Turns out, scutil --get ComputerName
does exactly what I want (outputs Tower of Nix
), but when I looked at the man page for scutil
, it stated:
HISTORY
The
scutil
command appeared in the Mac OS X Public Beta.
So it probably won't work on all operating systems such as Linux, *BSD, etc.
My question: How can I get the name of the computer on any (or nearly any) derivative of UNIX?
hostname
sounds more likehostname -i
to me. For completeness' sake, can you tryhostname -a
(show alias name) andhostname -s
(show short host name)? – Ulrich Schwarz Jul 31 '15 at 16:13alias | grep hostname
on your system? Because AFAIKhostname
is pretty universal across OSes (*nix, NetWare, Windows, ...) – Fabby Jul 31 '15 at 16:24hostname
:192.168.2.13
– Nebula Jul 31 '15 at 16:30$(scutil --get ComputerName 2>/dev/null || hostname)
. – Mark Plotnick Jul 31 '15 at 17:37