<tutorialjinni.com/>

bash get hostname

Posted Under: Linux, Shell, Snippets, Tutorials on Feb 16, 2021
Code snippet to get hostname in bash Linux shell. There are multiple ways to get hostname.

Reading hostname form file and storing in bash variable.
#! /bin/bash

var = $(cat /proc/sys/kernel/hostname)
echo var
#! /bin/bash

var = $(cat /etc/hostname)
echo var
Reading from command and copy its output in bash variable.
#! /bin/bash

var = $(hostname -f) # return portion before dot
echo var


imgae