unix command to extract part of a hostname

try the -s switch: hostname -s


Do you have the server name in a shell variable? Are you using a sh-like shell? If so,

${SERVERNAME%%.*}

will do what you want.


To build upon pilcrow's answer, no need for new variable, just use inbuilt $HOSTANME.

echo $HOSTNAME-->my.server.domain
echo ${HOSTNAME%%.*}-->my

Tested on two fairly different Linux's.

2.6.18-371.4.1.el5, GNU bash, version 3.2.25(1)-release (i386-redhat-linux-gnu) 3.4.76-65.111.amzn1.x86_64, GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)


You can use cut:

echo "testsrv1.main.corp.loc.domain.com" | cut -d"." -f1

Tags:

Unix