LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 09-09-2009, 07:53 AM   #1
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
[SOLVED] Bash: how does this $[ ] code snippet work?


Hello

Why does bash do this
Code:
c@CW8:~$ a=b
c@CW8:~$ b=c
c@CW8:~$ c=d
c@CW8:~$ set -o nounset
c@CW8:~$ echo $[a]
bash: d: unbound variable
c@CW8:~$ set +o nounset
c@CW8:~$ echo $[a]
0
c@CW8:~$ echo $[a+1]
1
Best

Charles

Last edited by catkin; 09-09-2009 at 12:35 PM.
 
Old 09-09-2009, 08:12 AM   #2
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Rep: Reputation: 37
hi catkin!

An unbound variable is one that has not been defined.

So lets review something...

Code:
[~]$ a=b
[~]$ echo $a
b
[~]$ echo ${a}
b
[~]$ echo $[a]
0
The only time i have seen [] in variables is in arrays like a[0] so maybe $[] is referencing a non existent array and is giving you the value 0.

If you are trying to reference to the "d" through "a" then i guess this would do the trick:

Code:
a=letter   # Variable "a" holds the name of another variable.
letter=z

echo

# Direct reference.
echo "a = $a"          # a = letter

# Indirect reference.
  eval a=\$$a
# ^^^        Forcing an eval(uation), and ...
#        ^   Escaping the first $ ...
# ------------------------------------------------------------------------
# The 'eval' forces an update of $a, sets it to the updated value of \$$a.
# So, we see why 'eval' so often shows up in indirect reference notation.
# ------------------------------------------------------------------------
  echo "Now a = $a"    # Now a = z
Now, remember that i am not an expert and this is just what i can see from the outside.

Im still learning.

Last edited by RaptorX; 09-09-2009 at 08:15 AM.
 
Old 09-09-2009, 12:34 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Thanks RaptorX

Solved it! After intensive netsearching (not helped by Google ignoring string "$[" in searches!) the bash man page gave the answer:
Code:
   Arithmetic Expansion
       Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result.  The format  for  arithmetic  expansion
       is:

              $((expression))

       The old format $[expression] is deprecated and will be removed in upcoming versions of bash.
Best

Charles
 
  


Reply

Tags
bash, variables



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
[SOLVED] [bash] code wont work - here document RaptorX Programming 6 08-17-2009 12:02 PM
how to make my code to work on bsd (pptp script on bash) jabka *BSD 1 05-01-2007 12:53 PM
Bug in c code calling bash code Linh Programming 11 08-12-2003 04:27 AM
How do I compile a code snippet. to test my box?? _Mhz Linux - Newbie 4 07-10-2002 11:03 AM
calendar-related code snippet request Citizen Bleys Programming 2 12-11-2001 12:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:25 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