LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-10-2011, 11:13 AM   #1
cijin
LQ Newbie
 
Registered: Nov 2011
Posts: 1

Rep: Reputation: Disabled
How can i use variables as the initial and final values of a variable in for loops


How can i use variables as the initial and final values of a variable in for loops
 
Old 11-10-2011, 11:15 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
In what language?
 
Old 11-10-2011, 11:38 AM   #3
sinuhe
Member
 
Registered: Apr 2010
Location: Utah
Distribution: Slackware
Posts: 47

Rep: Reputation: 4
posix shell for and variables

The first argument to for is defined as part of the for statement, but the word list could use a preset variable, which you could unset after the for loop.

Was that what you were asking, or did I misunderstand?
 
Old 11-10-2011, 08:44 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
How about showing us an example of what you're thinking? At least show us the inputs and the desired output.

The for loop syntax is quite simple:
Code:
for variable in word word word; do
	commands
done
The list of words is read after parameter expansion, so if you have variables containing words, or any other kind of substitution, then their contents will simply become part of the list.

Code:
var1='a b c'
var2='d e f'
echo 'foo bar baz' > file.txt

for var in $var1 "$var2" $( cat file.txt ) ; do
	echo "[$var]" 
done
Notice how quoting affects the result. The contents of a quoted variable will be seen as a single element, while an unquoted one will be word-split before the loop read it.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Loops in Bash shell or variable math... edpatterson Programming 4 10-29-2010 02:09 AM
Not able to invoke variable values vjayraghavan Programming 1 05-05-2010 03:22 AM
Values like VARIABLE[i] ? BlueSpirit Programming 5 04-27-2007 02:35 PM
read values of variables from a table osio Programming 7 01-11-2006 03:52 AM
variable with while loops michael_util Programming 4 08-16-2004 01:07 PM


All times are GMT -5. The time now is 02:13 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration