LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-22-2008, 03:56 PM   #1
Ricio
Member
 
Registered: Sep 2003
Location: Colombia
Distribution: Debian
Posts: 220

Rep: Reputation: 30
Question $HOSTNAME shell script not working


hello all,

well im learning bash scripting, and Im trying to write a few programs and all reading the beginners guide to bash from tldp.org.

Thing is that a script im doing does not show echo $HOSTNAME variable, which is weird cuz when I do echo $HOSTNAME on terminal it shows it but when I do it on the script does not display anything...
can anyone help me out?
 
Old 07-22-2008, 04:22 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Sometimes HOST or HOSTNAME may be set in the system wide profile, but you should not rely on it. Instead there is a program called hostname, which you can call and assign the output to a variable in your script like this:
Code:
HOST=$(hostname)
 
Old 07-22-2008, 05:12 PM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
HOSTNAME is set when you run bash, but not sh. Example:
Code:
$ cat ./showhostname 
#!/bin/sh

echo hostname: $HOSTNAME
$ ./showhostname        
hostname: 

$ cat ./showhostname 
#!/bin/bash

echo hostname: $HOSTNAME
$ ./showhostname        
hostname: myhost.example.com
 
Old 07-23-2008, 12:37 PM   #4
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
/bin/sh might not set it, but it shouldn't unset it if it's already set. Make sure it's exported and it will propagate into sub-shells. Put an

export HOSTNAME

in /etc/profile (after setting it as described by matthewg42, if the profile doesn't set it (it may be setting it but not exporting it)) and it will be inherited by all shells (unless the're specifically started with a clean environment, e.g. via env -i).
 
Old 07-23-2008, 02:18 PM   #5
Ricio
Member
 
Registered: Sep 2003
Location: Colombia
Distribution: Debian
Posts: 220

Original Poster
Rep: Reputation: 30
Great, thank you all... proved all 3 responses and they all do what I wanted to... Thank you!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using shell script to change hostname(Fedora Core6) lion1978 Linux - Software 10 03-05-2007 02:14 AM
my first shell script isn't working extendedping Programming 3 02-26-2007 02:23 PM
Running a shell script remotely via 'ssh <hostname> <command>' davee Linux - General 4 10-09-2005 01:38 AM
shell script not working sanjith11 Programming 2 07-07-2004 02:55 AM
why my shell script is not working linuxlah Linux - General 4 08-01-2002 08:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration