Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-21-2004, 11:15 AM   #1
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
Bash script

1) What is the ${#HOSTNAME} means ?
2) I think hostname "$HOSTNAME" means set the variable
hostname to whatever value is in HOSTNAME.
3) What is the difference between
hostname "$HOSTNAME" and hostname $HOSTNAME ?


if [${#HOSTNAME} -ne 0]; then
hostname "$HOSTNAME"
Old 04-21-2004, 02:02 PM   #2
Senior Member
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
1) I have never seen a variable written with a hash (#) in front, so all I can assume is that the variables name is "#HOSTNAME" but I am probably totally wrong on this one...

2) Actually, this is running the "hostname" program with the value of "$HOSTNAME" as an argument. Variable assignment uses the equals sign ie: hostname=$HOSTNAME

3) Quoting the variable preserves whitespace when referencing it. ie:
hello="A B  C   D"
echo $hello  
$ A B C D
echo "$hello" 
$ A B  C   D
If you haven't already, checkout the Advanced BASH Scripting Guide, it has everything you want to know about BASH scripting and lots of example code.

Last edited by bulliver; 04-21-2004 at 02:07 PM.
Old 04-21-2004, 03:20 PM   #3
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30

Thank you bulliver for your help.
Old 04-21-2004, 05:03 PM   #4
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: Bash script

Originally posted by Linh
1) What is the ${#HOSTNAME} means ?
It expands to the number of characters in the variable $HOSTNAME.
In other words: the length of $HOSTNAME.
For example if HOSTNAME is "Linh", then "echo $(#HOSTNAME)" prints "4".
If it is "Hko", then it prints "3".
Old 04-21-2004, 05:19 PM   #5
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30

Thank you HKo for your help


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
Bash script moodupani Programming 3 08-30-2005 07:31 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script brian0918 Programming 7 06-12-2003 06:06 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 AM.

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