LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 01:05 PM   #1
Leo Simon
LQ Newbie
 
Registered: Aug 2004
Posts: 22

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, 02: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 02:32 PM. Reason: $() instead of backticks
 
Old 08-13-2012, 02:33 PM   #3
YankeePride13
Member
 
Registered: Aug 2012
Distribution: Ubuntu 10.04, CentOS 6.3, Windows 7
Posts: 262

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

for filename in `ls -c1 *.help`; do
echo "$filename"
done
 
Old 08-13-2012, 02: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, 02:58 PM   #5
Leo Simon
LQ Newbie
 
Registered: Aug 2004
Posts: 22

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, 08:01 AM   #6
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
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, 11:43 AM   #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, 02:05 AM   #8
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
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



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
echo two command output in the same line chuikingman Linux - Software 11 05-06-2019 10:44 AM
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
Store multi-line output into an array in a Linux bash script steven.c.banks Linux - General 5 12-05-2010 02:08 PM
Store multi-line output into an array in a Linux bash script steven.c.banks Linux - General 2 12-17-2009 09:10 AM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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