Fedora Linux Support Community & Resources Center
  #1  
Old 2nd August 2009, 09:42 PM
troyatlarge Offline
Registered User
 
Join Date: Mar 2009
Location: Ely, NV. - 395 miles round trip to Wal-Mart
Posts: 333
linuxfedorafirefox
Seeking to understand string operator := Bash 101

From “learning the bash shell” I find the following:

“${varname:=word} If varname exist and isn't null, return its value; otherwise set it to word and then return its value” p. 91
Code:
So I write the following script:

#
#	seeking to understand  :=
#
#	string [arg1] 
#

arg1=$1

arg1=${arg1:="not given"}

echo $1

When I run like so, I get:
Code:
$ string test
test
But when I run it like this:
Code:
$ string
The echo prints a blank instead of behaving this way:
Code:
$ string
not given
I don't understand this as the book claims it will set it to word and then return its value. I thought at first that perhaps it was defaulting back to the actions of :- , but this is found not to be so for when I write the script this way:

Code:
#
#	seeking to understand  :=
#
#	string [arg1] 
#

arg1=$1{1:=“not given”}
and run it, it shows an error of incorrect assignment, as is expected if it is functioning correctly (can not set value of positional parameter). So what am I not understanding – that is, why does not the out put look like this:
Code:
$ string
not given
?????????????????

Last edited by troyatlarge; 2nd August 2009 at 09:50 PM. Reason: try to make it easier to read
Reply With Quote
  #2  
Old 2nd August 2009, 10:26 PM
adrianx Offline
Registered User
 
Join Date: Jul 2007
Location: South Africa
Posts: 954
linuxfedorafirefox
Quote:
Originally Posted by troyatlarge View Post
Code:
So I write the following script:

#
#    seeking to understand  :=
#
#    string [arg1] 
#

arg1=$1

arg1=${arg1:="not given"}

echo $1
That should be:
Code:
echo $arg1
__________________
Registered Linux User 460110
Reply With Quote
  #3  
Old 3rd August 2009, 01:38 AM
troyatlarge Offline
Registered User
 
Join Date: Mar 2009
Location: Ely, NV. - 395 miles round trip to Wal-Mart
Posts: 333
linuxfedorafirefox
Thank you.

The problem I ran into is that one can do this: arg1=$1 and later do this: arg1=${arg1:="not given"} and then, if arg1 is never typed it, agr1 does default to "not given" but never does $1 even though we said arg1=$1 to start with - it's like I was expecting a syllogism to hold up here when in fact no such thing takes place (a=b, b=c, thus a=c).

I now get the idea that positional parameters can not be so set from this sort of default assignment, although other variables can be. Furthermore, it seems rather reasonable to have such a thing - was just a matter of getting my brain around it to start with.

Thanks again.

Last edited by troyatlarge; 3rd August 2009 at 01:54 PM. Reason: as a moron, I woke up slightly
Reply With Quote
  #4  
Old 3rd August 2009, 03:49 PM
troyatlarge Offline
Registered User
 
Join Date: Mar 2009
Location: Ely, NV. - 395 miles round trip to Wal-Mart
Posts: 333
linuxfedorafirefox
Now I need to understand the difference between := and :- a little better. First I give my new script with notes and hope it shows some clarity in understanding:
Code:
#
#	the actions of  := in relation to positional parameters
#
#	string [arg1] 
#

arg1=$1

arg1=${arg1:="not given"} # has to be "${arg1...} not ${1....}

echo $arg1 
echo $1    

#
# Running this script with "string one", the result is;
#		$ string
#		one
#		one
# but when we run it as just "string" we get;
#		$ string
#		not given
#				 <--- a null entry is returned
# This shows that := "does" reassign the value 
# of a variable, but "does not" do so to the possitional parameter. 
# That is, the form of a=b, b=c, therefore a=c, does NOT
# apply here, .......  arg1=$1, arg1=${arg1:="not given"}, does not mean 
# "not given"=$1 even when arg1="not given". Instead "b" is resigned without that 
# assignment ever effecting "a" when "a" is a positional parameter.
#
Hopefully my thinking there is correct on the above.

My book states that :- and := are the same "except that it (:=) has the "side effect" of setting the value of the variable to the given word if the variable does not exist." However, given how the above script runs when := is replaced with :-,(it gives the same results as above) it seems as though :- is replacing the variable arg1 with "not given" on account of the fact that we did echo "$arg1" and got "not given" as a result - so clearly, I'm still not understanding the nature of these beast yet - I mean, what's the difference between :- and := ???

Last edited by troyatlarge; 3rd August 2009 at 08:43 PM.
Reply With Quote
  #5  
Old 4th August 2009, 03:16 PM
troyatlarge Offline
Registered User
 
Join Date: Mar 2009
Location: Ely, NV. - 395 miles round trip to Wal-Mart
Posts: 333
linuxfedorafirefox
OK - I think I figured out the width of difference between the :- and := string operators. I don't know that it is ok to post like this, but, in case anyone happens along who this may be of help to - or for additional comments on, I give the following :
Code:
#
#		string2
#
#	understanding difference between :- and :=
#
#	string2 [x]
#
        # running program by the input "string2"
	# first test with variable "y"

	x=${y:-"not given"}	# for input "string2"
	echo $x			# returns "not given"
	echo $y			# returns " "	(:- assigns to x, but not y)
	
	x=${y:="not given2"}
	echo $x			# returns "not given2"
	echo $y			# returns "not given2"  (:= assigns to x and y)

	# now test with variable equal to a positional parameter

	x=${1:-"not given3"}
	echo $x			# returns "not given3"
	echo $y			# returns "not given2" (:- assigns to x, but not y)
	
	x=${1:="not given4"}	# returns error message "$1: cannot assign ..."
	echo $x			# returns "not given3" (error exits without change)
	echo $y			# returns "not given2" 
#
#	:= reassigns value of the variable on the right side of the "=" as well
#	as on the left side. However, one cannot assign positional parameters in
#	such ways so it fails when a positional parameter is given as the variable.
#

Last edited by troyatlarge; 4th August 2009 at 03:17 PM. Reason: left one thing out
Reply With Quote
Reply

Tags
101, bash, operator, seeking, string, understand

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding \n newline control character onto a bash string CD-RW Programming & Packaging 4 2nd July 2009 11:49 AM
server glx vendor string: SGI, server glx version string: 1.2 + ATI X1950 akp Using Fedora 1 2nd September 2007 02:56 AM
sed regex string replacement after match string issue adosch Using Fedora 10 29th June 2007 04:07 PM
What is the operator * ./. . . * royeo Using Fedora 1 4th September 2006 12:13 AM
Bash help: Find first n characters in a string? leaded Programming & Packaging 3 29th December 2004 05:53 PM


Current GMT-time: 21:55 (Tuesday, 30-09-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat