LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 08-13-2012, 02:05 PM   #1
Leo Simon
LQ Newbie
 
Registered: Aug 2004
Posts: 21

Rep: Reputation: 0
multi-line output from a bash script using echo and tr


I want to do an ls -c1 from within a bash script, and output the result with one line per entry.

The command below returns a single very long line

#!/bin/bash
files=`ls -c1 *.help`
echo $files

The command below works fine from the command line (in tcsh)

echo `ls -c1 *.help` | tr "[:space:]" "\n"

But when I put it in a script (with any kind of shell) it returns just the first entry in $files

#!/bin/bash
files=`ls -c1 *.help` | tr "[:space:]" "\n"
echo $files

Any help would be most appreciated
 
Old 08-13-2012, 03:28 PM   #2
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi,

It's simple, you're missing " (double quotes) around the variable you're echo-ing
Code:
#!/bin/bash
files=$(ls -c1 *.help | tr "[:space:]" "\n")
echo "$files"
and use the $(command) syntax instead of `command` (backtick).

Last edited by lithos; 08-13-2012 at 03:32 PM. Reason: $() instead of backticks
 
Old 08-13-2012, 03:33 PM   #3
YankeePride13
Member
 
Registered: Aug 2012
Distribution: Ubuntu 10.04, CentOS 6.3, Windows 7
Posts: 228

Rep: Reputation: 51
Try putting it in a loop:

for filename in `ls -c1 *.help`; do
echo "$filename"
done
 
Old 08-13-2012, 03:54 PM   #4
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
Quote:
Originally Posted by YankeePride13 View Post
Try putting it in a loop:

for filename in `ls -c1 *.help`; do
echo "$filename"
done

bash can read from the file system already.
Code:
for f in *.help
do
  echo $f
done
 
Old 08-13-2012, 03:58 PM   #5
Leo Simon
LQ Newbie
 
Registered: Aug 2004
Posts: 21

Original Poster
Rep: Reputation: 0
Thanks to all

For your quick responses. They all work, but I liked lithos's solution the best. It's amazing how many times double quotes fixes things, and more amazing (i.e., pathetic) that I never remember this
 
Old 08-14-2012, 09:01 AM   #6
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 669

Rep: Reputation: 134Reputation: 134
Quote:
Originally Posted by Leo Simon View Post
For your quick responses. They all work, but I liked lithos's solution the best. It's amazing how many times double quotes fixes things, and more amazing (i.e., pathetic) that I never remember this
If sorting by ctime is not that important for you, I think trey85stang's solution is better:
1. It avoid quite redundant invocation of external programs
2. It will works even if filenames have spaces. lithos's solution will break parts of such filenames to separate lines.
 
Old 08-14-2012, 12:43 PM   #7
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
Originally Posted by Valery Reznic View Post
If sorting by ctime is not that important for you, I think trey85stang's solution is better:
1. It avoid quite redundant invocation of external programs
2. It will works even if filenames have spaces. lithos's solution will break parts of such filenames to separate lines.
Good point!
I was merely correcting Leo's line "echo `ls -c1 *.help` | tr "[:space:]" "\n""
to make echo print out with lines.
 
Old 08-15-2012, 03:05 AM   #8
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 669

Rep: Reputation: 134Reputation: 134
Quote:
Originally Posted by lithos View Post
Good point!
I was merely correcting Leo's line "echo `ls -c1 *.help` | tr "[:space:]" "\n""
to make echo print out with lines.
Sure, there is a great value in explaining where error is and why some code doesn't work as expected
 
  


Reply

Tags
echo, tr


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 07:56 AM
echo two command output in the same line chuikingman Linux - Software 8 03-31-2011 07:20 PM
Store multi-line output into an array in a Linux bash script steven.c.banks Linux - General 5 12-05-2010 03:08 PM
Store multi-line output into an array in a Linux bash script steven.c.banks Linux - General 2 12-17-2009 10:10 AM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 01:08 AM


All times are GMT -5. The time now is 04:02 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration