LinuxQuestions.org
Visit Jeremy's Blog.
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 04-25-2013, 09:48 PM   #1
user1x
LQ Newbie
 
Registered: Apr 2013
Posts: 14

Rep: Reputation: Disabled
How to change the delimiter when I do a "${myarray[@]}" in Bash?


I have an array of file paths that I want to process with a single command. Some of the filenames have a space. So I can't do this:

echo "${myarray[@]}"|xargs myoperation

I'd like to use `xargs -0` but I can't figure out how to make the output of the array delimited by a null character instead of a space. Any ideas?
 
Old 04-26-2013, 12:20 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Try looking up the IFS variable.
 
Old 04-26-2013, 08:05 AM   #3
user1x
LQ Newbie
 
Registered: Apr 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
IFS doesn't seem to help:
x=("a" "b 2" "c");IFS=$'\0' echo "${x[@]}"|xxd

gives me:
0000000: 6120 6220 3220 630a a b 2 c.

Maybe I'm just using it wrong? This is in Ubuntu 12 by the way.
 
Old 04-26-2013, 09:16 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Here's a short example
Code:
a=(a "b 2" c)
for i in "${a[@]}"
do
	echo $i
done

# output
a
b 2
c
 
Old 04-26-2013, 09:48 AM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
printf can insert the null character after each array element.
Code:
printf '%s\0' "${myarray[@]}" | xargs -0 myoperation
 
1 members found this post helpful.
Old 04-26-2013, 10:58 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by user1x View Post
I have an array of file paths that I want to process with a single command. Some of the filenames have a space. So I can't do this:

echo "${myarray[@]}"|xargs myoperation

I'd like to use `xargs -0` but I can't figure out how to make the output of the array delimited by a null character instead of a space. Any ideas?
Maybe I misunderstood your question, but why don't you just do

Code:
myoperation "${myarray[@]}"

Last edited by millgates; 04-26-2013 at 11:06 AM.
 
Old 04-26-2013, 02:04 PM   #7
user1x
LQ Newbie
 
Registered: Apr 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
Maybe I misunderstood your question, but why don't you just do

Code:
myoperation "${myarray[@]}"
Good question. I have potentially thousands of files. It will have an command line limit overflow.
 
Old 04-26-2013, 02:06 PM   #8
user1x
LQ Newbie
 
Registered: Apr 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by Kenhelm View Post
printf can insert the null character after each array element.
Code:
printf '%s\0' "${myarray[@]}" | xargs -0 myoperation
Perfect. That will work!

x=("a" "b 2" "c");printf '%s\0' "${x[@]}"|xxd

gives me:
0000000: 6100 6220 3200 6300 a.b 2.c.
 
Old 04-29-2013, 10:28 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I agree that printf+xargs is generally the way to go, but another option would be to splice the array with a c-style for loop, to limit the number of entries processed at once.

Code:
for (( i=0; i<${#array[@]} ; i+=50 )); do

    mycommand "${array[@]:i:50}"

done
This will run the command on 50 entries at a time.
 
  


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 02:03 AM
[SOLVED] how do i change "bash-4.1#" to "user@hostname try-alls Slackware 18 08-22-2010 04:33 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
C/C++ functions similar to BASH's "cp", "mv", "mkdir", etc? kornerr Programming 10 04-23-2006 10:48 AM
Change buffer delimiter from "\n" SaTaN Programming 14 10-11-2003 10:33 AM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration