LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-01-2010, 01:30 PM   #1
coffee arabia
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Rep: Reputation: 0
Question Display Array of Strings with White Spaces


Hello, this is a C shell question.

I generated an array of strings, but each string has white spaces between tokens. My script is

set cmd1 = 'apple orange'
set cmd2 = 'tea coffee'
set cmd_list = ($cmd1 $cmd2)

The problem is when I tried to display each element from that array

foreach cmd ($cmd_list)
echo "cmd_list is $cmd"
end

What I want to see is

cmd_list is apple orange
cmd_list is tea coffee

but instead I saw

cmd_list is apple
cmd_list is orange
cmd_list is tea
cmd_list is coffee

How should I write the foreach command to fix the problem?

Thanks
 
Old 10-01-2010, 02:27 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can try the :q modifier to prevent word splitting. For example:
Code:
#!/bin/tcsh
set cmd1 = "apple orange"
set cmd2 = "tea coffee"
set cmd_list = ($cmd1:q $cmd2:q)

foreach cmd ($cmd_list:q)
  echo "cmd_list is $cmd"
end
should do the trick. Please, note that this cannot be called array (as far as I know the C-shell does not manage arrays) but a list. Hope this helps.

Ah... and welcome to LinuxQuestions!

Last edited by colucix; 10-01-2010 at 02:35 PM.
 
Old 10-01-2010, 04:24 PM   #3
coffee arabia
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Talking Thanks!

It works!
 
Old 10-02-2010, 12:48 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please mark as SOLVED if you have your solution.
 
  


Reply



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
unix command to remove white spaces naqiboy Programming 15 03-14-2012 04:25 AM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
removing white spaces in the file tucs_123 Linux - Newbie 9 01-03-2009 10:28 AM
regular expressions ~ white spaces stevie_velvet Programming 3 07-15-2006 10:28 AM
how to get rid of the input beginning white spaces feetyouwell Programming 3 09-30-2004 01:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:35 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
Open Source Consulting | Domain Registration