Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-26-2007, 01:23 AM   #1
Senior Member
Registered: Nov 2006
Distribution: Debian Squeeze SMP AMD64
Posts: 3,318

Rep: Reputation: 126Reputation: 126
Shell string concatenation

How do I concatenation two strings in a shell script. I've been searching for an hour and I simply cannot find a good shell programming guide online.

What I'm trying to do is like the following:

list1="dev sys proc"
for i in $list1
(some string operation here)

When all is said and done, I want list2 to contain "dev\|sys\|proc". Please set aside the idea of just setting the value of list2 to "dev\|sys\|proc" in the first place. I have other things I want to do with list1 in another loop. I'm sure there's some sed command to do it, but I'd rather see some way involving a string concatenation.

Sorry if my frustration shows through in this post.
Old 04-26-2007, 01:35 AM   #2
Senior Member
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
i thought you could concatenate strings by doing something like
myStr = "joe"
myStr = $mystr bob
at school we use csh and this would work--i assume your using bash? i dont know it

Last edited by nadroj; 04-26-2007 at 01:43 AM.
Old 04-26-2007, 10:30 AM   #3
LQ Guru
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 75
Moved: This thread is more suitable in the Programming forum and has been moved accordingly to help your thread/question get the exposure it deserves.
Old 04-26-2007, 11:49 AM   #4
Registered: Feb 2007
Location: $HOME
Distribution: Hardened Gentoo
Posts: 66
Blog Entries: 1

Rep: Reputation: 16

list1="dev sys proc"
list2="$(echo $list1 | sed 's/\ /\\|/g')"

echo "list1 is: $list1"
echo "list2 is: $list2"
list1 is: dev sys proc
list2 is: dev\|sys\|proc

Last edited by omnio; 04-26-2007 at 12:11 PM.
Old 04-26-2007, 12:11 PM   #5
Senior Member
Registered: Nov 2006
Distribution: Debian Squeeze SMP AMD64
Posts: 3,318

Original Poster
Rep: Reputation: 126Reputation: 126
Ah, omnio, thanks. You gave me the hint to make it work:
list1="data data2 proc dev sys musem wendy"
for i in $list1
if test $first -eq 1
list2="$echo $list2\|$i"
ls / | grep -vw $list2


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
String Manipulations in Shell Script Programming 15 04-19-2007 11:40 AM
string concatenation in c/c++ for a socket program mohtasham1983 Programming 3 02-14-2007 06:42 PM
String in Bash Shell nguyenzoro Linux - Newbie 4 11-19-2005 05:49 PM
string shell command? minm Linux - Newbie 10 08-19-2004 11:09 AM
string concatenation in AWK xanthium Programming 1 04-22-2002 05:41 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration