Rose Perrone
Rose Perrone

Reputation: 63546

How can I find which host I'm on within my .bashrc?

I've created a git repository for my .bashrc that I pull from in my local machine and a remote machine. I want to run some commands for my localhost and not for my remote host.

Upvotes: 1

Views: 663

Answers (3)

chepner
chepner

Reputation: 531055

Since you are asking about .bashrc in particular, may I assume you are using bash? bash already sets the parameter HOSTNAME automatically.

Upvotes: 0

arielf
arielf

Reputation: 5952

For a more general solution, which:

  • works in both bash and sh
  • supports file pattern matching for the hostnames

you may use this idiom:

#!/bin/bash
HOST=`hostname`
case "$HOST" in
   # examples of prefix matches:
   host1*) echo this matches host1 ;;
   host2*) echo this matches host2 ;;
   # and so on...

   # if none matched you use the default '*' pattern
   # which matches everything else
   *)     echo none matched: hostname is $HOST ;;
esac

Upvotes: 0

Rose Perrone
Rose Perrone

Reputation: 63546

This worked for me:

HOSTNAME=$(hostname)
if [ ${HOSTNAME} == "ros-mbp.local" ]; then
  echo "host is local"
elif [ ${HOSTNAME} == "dev843.prn1.facebook.com" ]; then
  echo "host is remote"
else
  echo "host doesn't match."
fi

Upvotes: 3

Related Questions