LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-07-2012, 12:39 AM   #1
Ztcoracat
LQ Guru
 
Registered: Dec 2011
Distribution: Slackware, MX 18
Posts: 9,484
Blog Entries: 15

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Understand Shell Script Guide Example


Hi:

I've been studying this website on Shell Scripting for about a week and still have a way to go but have a few questions to the examples given.

I'm in the section of 'variable types' approx. 1/2 way down the page-
http://mywiki.wooledge.org/BashGuide/Parameters

Just before the example given it states that expansion happens when a parameter is prefixed by a $ sign- Here's part of the example that I don't understand:
Code:
$ a=5; a+=2 echo $a; unset a
52
$ a=5; let a+=2 echo $a; unseta
7
Ok; i see in the first example where expansion happened because it was prefixed y a $ sign. But the second line was prefixed as well but the work 'let' was introduced.
Was it because of the word 'let' used in the expression that allowed the normal value of 5 plus 2?


The article said" Integer: declare -i variable: holds an integer Assigning values to this variable automatically triggers Arithmetic Evaluation"

When numbers are used with variables and (expansion) that include words that allow the expression to be multiplied or added; in the future what is the indication besides the word 'let' that gives the indication of which kind of arithmetic is implied?

If it were you would you just print out the whole Guide?
My hand started giving me pain from all the writing-

Last edited by Ztcoracat; 10-07-2012 at 12:40 AM. Reason: proper punctuation
 
Old 10-07-2012, 02:25 AM   #2
ozanbaba
Member
 
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778

Rep: Reputation: 135Reputation: 135
let allowed arithmetic expression to work.
 
1 members found this post helpful.
Old 10-07-2012, 02:59 AM   #3
Ztcoracat
LQ Guru
 
Registered: Dec 2011
Distribution: Slackware, MX 18
Posts: 9,484

Original Poster
Blog Entries: 15

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Thanks Ozan.

Anyones else have another view on this?
 
Old 10-07-2012, 04:21 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Never ran into this before....

Apparently the "+=" operator acts to append characters to a string---unless the "let" command is used. BASH allows only integer arithmetic and strings are treated as numbers depending on context.
 
Old 10-07-2012, 09:57 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Yes, variables in the shell are untyped and they are usually treated as strings unless:
a. they are undeclared but used in an arithemtic context (let, expr or the arithemtic operator)
b. they are explicitly declared using the declare or the typeset built-ins.

The += operator is thought as an arithmetic operator, but if used outside an arithmetic context it simply adds a string to another (as pixellany already noticed) that is it performs string concatenation. However you will not see it used in such a context, because a) it is more intuitively an arithmetic operator and b) string concatenation is accomplished by putting variables and strings side by side, e.g.
Code:
a=${a}"concatenated string"
 
Old 10-08-2012, 11:33 AM   #6
Ztcoracat
LQ Guru
 
Registered: Dec 2011
Distribution: Slackware, MX 18
Posts: 9,484

Original Poster
Blog Entries: 15

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
I looked up append-
- to join or add to something-
http://en.wikipedia.org/wiki/Append
Now I understand what BASH does with an integar and strings with numbers-

Than I looked up concatenation-
- operation of joining 2 character strings end to end
http://en.wikipedia.org/wiki/Concatenation

At first this was a little hard to get but I have it now.
The explanation "yes, variables in the shell are untyped and treated as strings unless"(a) & (b)

How you both explained helped. Now I have rules to go by.

Thank You both!

Last edited by Ztcoracat; 10-08-2012 at 11:34 AM.
 
Old 10-08-2012, 12:51 PM   #7
Ztcoracat
LQ Guru
 
Registered: Dec 2011
Distribution: Slackware, MX 18
Posts: 9,484

Original Poster
Blog Entries: 15

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
BASH takes the parameters value and replaces the parameters expansion by it's value before executing the command. (I didn't know) that this is also considered a substitution. Much more I have to remember that there are special parameters as well.(that are not variables)

I think for me; understanding what BASH does (how it interprets and what it does with expressions/arguments/commands) makes more sense at this point to me than understand how to write a script.

I haven't gotten past #!bin/bash. Still have to read more about the How To but I'll finish this article first.

It's good to have help; Thanks again
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell Script - Need Help to Understand walidsi AIX 6 10-11-2012 03:28 PM
Help needed to understand a bit of shell script yogeshm02 Programming 8 02-03-2008 12:05 PM
Help me understand this shell script dwhitney67 Linux - Software 2 10-18-2007 09:02 AM
understand variable type in shell script qrshat Programming 5 09-09-2006 02:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:48 AM.

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