31

I would like to extract the first part of this hostname testsrv1 from testsrv1.main.corp.loc.domain.com in UNIX, within a shell script.

What command can I use? It would be anything before the first period .

8 Answers 8

33

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.

0
30

You can use cut:

echo "testsrv1.main.corp.loc.domain.com" | cut -d"." -f1
1
  • 2
    This isn't really doing it within a shell script as much as asking cut to do it.
    – monokrome
    Commented Feb 14, 2019 at 5:08
27

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)

3
  • 2
    This is assuming the question is about the hostname of the server the shell is running on. If it was, then this would be correct - and is probably helpful for some users who come across this question/answer. But, as asked, this answer is actually wrong.
    – zaTricky
    Commented Jan 12, 2017 at 12:53
  • Under macOS it is HOST.
    – Lenar Hoyt
    Commented Sep 19, 2017 at 21:32
  • 1
    @zaTricky Agreed. Sometimes as IT people we have to interpret what people ask vs what they actually need. I'm glad a few people found my answer helpful. The nice thing about unix is there are always several ways of doing something - none wrong - but some are prettier than others. Commented Oct 11, 2018 at 9:48
27

try the -s switch: hostname -s

1
  • perfect! To me it seems significantly cleaner than just cutting away based on .'s.
    – André
    Commented Feb 19, 2021 at 12:56
5

I use command cut, awk, sed or bash variables

Operation

Via cut

[flying@lempstacker ~]$ echo "testsrv1.main.corp.loc.domain.com" | cut -d. -f1
testsrv1
[flying@lempstacker ~]$

Via awk

[flying@lempstacker ~]$ echo "testsrv1.main.corp.loc.domain.com" | awk -v FS='.' '{print $1}'
testsrv1
[flying@lempstacker ~]$

Via sed

[flying@lempstacker ~]$ echo "testsrv1.main.corp.loc.domain.com" | sed -r 's@([^.]*).(.*)@\1@g'
testsrv1
[flying@lempstacker ~]$

Via Bash Variables

[flying@lempstacker ~]$ hostName='testsrv1.main.corp.loc.domain.com'
[flying@lempstacker ~]$ echo ${hostName%%.*}
testsrv1
[flying@lempstacker ~]$
1

You could have used "uname -n" to just get the hostname only.

1
  • 1
    This makes the assumption that the host string being parsed is always for the host the code is executing on. Since the OP has not specified that, the OP probably wants to be able to do that for any FQDN string, so your answer would not apply.
    – Michael M
    Commented Dec 6, 2017 at 20:05
1

You can use IFS to split text by whichever token you want. For domain names, we can use the dot/period character.

#!/usr/bin/env sh

shorthost() {
  # Set IFS to dot, so that we can split $@ on dots instead of spaces.
  local IFS='.'

  # Break up arguments passed to shorthost so that each domain zone is
  # a new index in an array.
  zones=($@)

  # Echo out our first zone
  echo ${zones[0]}
}

If this is in your script then, for instance, you'll get test when you run shorthost test.example.com. You can adjust this to fit your use case, but knowing how to break the zones into the array is the big thing here, I think.

I wanted to provide this solution, because I feel like spawning another process is overkill when you can do it easily and completely within your shell with IFS. One thing to watch out for is that some users will recommend doing things like hostname -s, but that doesn't work in the BSD userland. For instance, MacOS users don't have the -s flag, I don't think.

0

Assuming the variable $HOSTNAME exists, so try echo ${HOSTNAME%%.*} to get the top-most part of the full-qualified hostname. Hope it helps.

If interested, the hint is from the below quoted partial /etc/bashrc on a REHL7 host:

      if [ -e /etc/sysconfig/bash-prompt-screen ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
      else
          PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
      fi
      ;; ... ```

Not the answer you're looking for? Browse other questions tagged or ask your own question.