LinuxQuestions.org
Review your favorite Linux distribution.
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, 08: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-25-2013, 11:20 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Try looking up the IFS variable.
 
Old 04-26-2013, 07: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, 08:16 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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, 08:48 AM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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, 09:58 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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 10:06 AM.
 
Old 04-26-2013, 01: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, 01: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, 09:28 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 01:03 AM
[SOLVED] how do i change "bash-4.1#" to "user@hostname try-alls Slackware 18 08-22-2010 03:33 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
C/C++ functions similar to BASH's "cp", "mv", "mkdir", etc? kornerr Programming 10 04-23-2006 09:48 AM
Change buffer delimiter from "\n" SaTaN Programming 14 10-11-2003 09:33 AM

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

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