LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-13-2010, 09:25 AM   #1
Beaverfriend
LQ Newbie
 
Registered: Feb 2010
Posts: 7

Rep: Reputation: 0
Bash: Problem to prepend text to variable


The output of following code is not like it's intended ...
Code:
echo "Test prepending ..."

for words in "apple" "hour"
	do
		prepended="an $prepended $(echo "$words")"
done

echo $prepended
echo "---"


echo "Test appending ..."
for words in "apple" "hour"
	do
		appending=("$appending $(echo "$words") is a nice word,")
done

echo $appending
This is the output:
Code:
Test appending ...
an an apple hour
---
Test prepending ...
apple is a nice word, hour is a nice word,
But of course what I want to do in the first set of commands is to prepend the word "an" to the words "apple" and "hour" in the for-loop.

Would be very grateful for any clues! Thanks in advance!

Last edited by Beaverfriend; 02-13-2010 at 09:44 AM. Reason: typo
 
Old 02-13-2010, 09:44 AM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 110Reputation: 110
This has several issues:
1. Misuse of quotes. You don't need to quote the items to loop through, but you *do* need to quote any expansion (echo "$appended" and echo "$prepended")
2. Useless use of echo. "$(echo "$words")" is equivalent to just "$words"
3. The prepended=("...") construct creates an *array* of one item and assigns it to the variable prepended. Probably not what you want to do.

Please throw out whatever it is you're trying to use to learn and read this:
http://mywiki.wooledge.org/BashGuide
 
1 members found this post helpful.
Old 02-13-2010, 09:45 AM   #3
Beaverfriend
LQ Newbie
 
Registered: Feb 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Sorry for confusing prepend with append. See edit in last post.
 
Old 02-13-2010, 09:53 AM   #4
Beaverfriend
LQ Newbie
 
Registered: Feb 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks for your reply!

Actually, the stupid example is my own invention

What would be a working way to prepend a string to the items in the for-loop?
 
Old 02-13-2010, 10:12 AM   #5
Beaverfriend
LQ Newbie
 
Registered: Feb 2010
Posts: 7

Original Poster
Rep: Reputation: 0
This works as intended:

Code:
for words in apple hour
    do
        prepended="$prepended an $words"
done

echo $prepended
Problem was that I was putting the word to prepend ("an") before the $prepended-variable, which contains the collected that has already had "an" prepended to them. What I wanted to do was to prepend earlier prependations to the result of the latest. Code above does that.

Removed useless echos and quotes as tuxdev suggested.
 
  


Reply

Tags
bash, loop


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
want to prepend to a text file, not append urentity Programming 7 08-03-2009 12:45 AM
variable from text file bash drumbum Programming 1 07-30-2009 09:50 AM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
text formatting in bash variable David the H. Programming 10 05-09-2008 12:02 PM
Getting a variable from a text-file (bash) PatrickBecks Programming 4 02-14-2008 08:52 PM


All times are GMT -5. The time now is 11:18 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration