LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script (https://www.linuxquestions.org/questions/programming-9/bash-script-172901/)

Linh 04-21-2004 11:15 AM

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"
fi

bulliver 04-21-2004 02:02 PM

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:
Code:

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.
http://www.tldp.org/LDP/abs/html/index.html

Linh 04-21-2004 03:20 PM

reply
 
Thank you bulliver for your help.

Hko 04-21-2004 05:03 PM

Re: Bash script
 
Quote:

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".

Linh 04-21-2004 05:19 PM

reply
 
Thank you HKo for your help


All times are GMT -5. The time now is 07:10 AM.