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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-22-2008, 03:56 PM
|
#1
|
Member
Registered: Sep 2003
Location: Colombia
Distribution: Debian
Posts: 220
Rep:
|
$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?
|
|
|
07-22-2008, 04:22 PM
|
#2
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
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:
|
|
|
07-22-2008, 05:12 PM
|
#3
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
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
|
|
|
07-23-2008, 12:37 PM
|
#4
|
Member
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81
Rep:
|
/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).
|
|
|
07-23-2008, 02:18 PM
|
#5
|
Member
Registered: Sep 2003
Location: Colombia
Distribution: Debian
Posts: 220
Original Poster
Rep:
|
Great, thank you all... proved all 3 responses and they all do what I wanted to... Thank you!
|
|
|
All times are GMT -5. The time now is 07:06 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|