LinuxQuestions.org
Visit Jeremy's Blog.
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 04-26-2007, 12:23 AM   #1
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Squeeze 2.6.32.9 SMP AMD64
Posts: 3,238

Rep: Reputation: 121Reputation: 121
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"
list2=
for i in $list1
do
(some string operation here)
done

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, 12:35 AM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
i thought you could concatenate strings by doing something like
Code:
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 12:43 AM.
 
Old 04-26-2007, 09:30 AM   #3
reddazz
Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 73
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, 10:49 AM   #4
omnio
Member
 
Registered: Feb 2007
Location: $HOME
Distribution: Hardened Gentoo
Posts: 66
Blog Entries: 1

Rep: Reputation: 16
Code:
#!/bin/bash

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 11:11 AM.
 
Old 04-26-2007, 11:11 AM   #5
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Squeeze 2.6.32.9 SMP AMD64
Posts: 3,238

Original Poster
Rep: Reputation: 121Reputation: 121
Ah, omnio, thanks. You gave me the hint to make it work:
Code:
#!/bin/sh
list1="data data2 proc dev sys musem wendy"
list2=""
first=1
for i in $list1
do
if test $first -eq 1
then
first=0
list2="$i"
fi
list2="$echo $list2\|$i"
done
ls / | grep -vw $list2
 
  


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


All times are GMT -5. The time now is 01:41 PM.

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