LinuxQuestions.org
Review your favorite Linux distribution.
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 12-05-2016, 01:47 PM   #1
ernstlx
LQ Newbie
 
Registered: Feb 2011
Location: Vienna, Austria
Distribution: Debian
Posts: 13

Rep: Reputation: 6
BASH: How can I get an array with ls-output containing blanks?


Hello!

I like to get an array with every path containing a file ".tag" in the sub-subdirectory:

Code:
TAGPATH="*/*/.tag"
INDX=(`ls -Q $TAGPATH`)
The path to the files contains blanks and this is reason why it fails. I get an array, but regardless of the quotation (single, double or none) it results in array elements only from one blank to the next.

I've tried every ls option I know, tried to manipulate the output with sed to escape the blank characters, but neither helped. Without blanks it's no problem, but with blanks it seems to be impossible.

Can someone help me solving this problem?

Thanks in advance!
 
Old 12-05-2016, 02:05 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
If you were to get the paths into an array, you would be able to do this:

Code:
PATHS=("/tmp/what ever/this1"
       "/tmp/what ever/this2"
       "/tmp/what ever/this3"
       "/tmp/what ever/this4")
IFS=""
for p in ${PATHS[*]}
do
    echo "${p}"
done
Is that what you are looking for?

Edit: Or, now that I'm re-reading it, are you looking for how to get the paths into the array in the first place?

If so, just use IFS to call out \n\t as field seperators, and ignore spaces, while you are importing it into the array.

Code:
IFS=$'\t\n'
TAGPATH="*/*/.tag"
INDX=(`ls -Q $TAGPATH`)

Last edited by szboardstretcher; 12-05-2016 at 02:11 PM.
 
1 members found this post helpful.
Old 12-05-2016, 02:20 PM   #3
ernstlx
LQ Newbie
 
Registered: Feb 2011
Location: Vienna, Austria
Distribution: Debian
Posts: 13

Original Poster
Rep: Reputation: 6
$IFS does the trick!

Thank you very much!!!

Greetings from Vienna!
Ernst
 
Old 12-06-2016, 09:06 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,770

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
It's a lot easier if you don't use ls:
Code:
TAGPATH="*/*/.tag"
INDX=($TAGPATH)
Pathname expansion occurs after word splitting, so embedded spaces in the names are taken literally and not as word separators.

It is almost always a bad idea to process the output from ls. That output is primarily intended for human consumption.
 
1 members found this post helpful.
Old 12-10-2016, 09:31 AM   #5
Fat_Elvis
Member
 
Registered: Oct 2016
Distribution: FreeDOS 1.2
Posts: 309

Rep: Reputation: 92
Quote:
Originally Posted by rknichols View Post
It's a lot easier if you don't use ls:
Code:
TAGPATH="*/*/.tag"
INDX=($TAGPATH)
Pathname expansion occurs after word splitting, so embedded spaces in the names are taken literally and not as word separators.

It is almost always a bad idea to process the output from ls. That output is primarily intended for human consumption.
Oh wow. This is actually very useful info. Thanks.

Last edited by Fat_Elvis; 12-10-2016 at 09:33 AM.
 
  


Reply

Tags
array, bash, blank, ls


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
[SOLVED] Bash script - output of command into array. systemlordanubis Programming 2 05-02-2014 11:22 PM
[Bash] array `ls` output aoresearch Linux - Newbie 5 11-11-2009 08:21 AM
Awk output to bash array? kj6loh Programming 4 09-07-2009 12:36 PM
bash: put output from `ls` into an array samel_tvom Programming 18 07-31-2005 12:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:25 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