Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
bash script; prepending text to each item in a $VAR
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Souperman
Guru
Guru


Joined: 14 Jul 2003
Posts: 449
Location: Cape Town, South Africa

PostPosted: Fri Oct 24, 2003 10:00 am    Post subject: bash script; prepending text to each item in a $VAR Reply with quote

Brain is dysfunctional today. :wink:

Say I have $SOMEVAR="foo bar baz", what's the easiest way to prepend something (e.g. 'x') to each item so that I end up with a string like "xfoo xbar xbaz"?
_________________
moo
Back to top
View user's profile Send private message
far
Guru
Guru


Joined: 10 Mar 2003
Posts: 394
Location: Stockholm, Sweden

PostPosted: Fri Oct 24, 2003 10:47 am    Post subject: Reply with quote

Code:
SOMEVAR=$(for x in $SOMEVAR; do echo -n "x$x "; done)

_________________
The Porthole Portage Frontend
Back to top
View user's profile Send private message
far
Guru
Guru


Joined: 10 Mar 2003
Posts: 394
Location: Stockholm, Sweden

PostPosted: Fri Oct 24, 2003 10:54 am    Post subject: Reply with quote

or
Code:
SOMEVAR=$(echo $SOMEVAR | perl -p -e "s/\b\w/x$&/g")

_________________
The Porthole Portage Frontend
Back to top
View user's profile Send private message
Souperman
Guru
Guru


Joined: 14 Jul 2003
Posts: 449
Location: Cape Town, South Africa

PostPosted: Fri Oct 24, 2003 11:04 am    Post subject: Reply with quote

far wrote:
Code:
SOMEVAR=$(for x in $SOMEVAR; do echo -n "x$x "; done)

Perfect, thanks! :)
_________________
moo
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum