LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   multi-line output from a bash script using echo and tr (https://www.linuxquestions.org/questions/linux-general-1/multi-line-output-from-a-bash-script-using-echo-and-tr-4175421892/)

Leo Simon 08-13-2012 01:05 PM

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

lithos 08-13-2012 02:28 PM

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).

YankeePride13 08-13-2012 02:33 PM

Try putting it in a loop:

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

trey85stang 08-13-2012 02:54 PM

Quote:

Originally Posted by YankeePride13 (Post 4753334)
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


Leo Simon 08-13-2012 02:58 PM

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

Valery Reznic 08-14-2012 08:01 AM

Quote:

Originally Posted by Leo Simon (Post 4753347)
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.

lithos 08-14-2012 11:43 AM

Quote:

Originally Posted by Valery Reznic (Post 4753885)
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.

Valery Reznic 08-15-2012 02:05 AM

Quote:

Originally Posted by lithos (Post 4754007)
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


All times are GMT -5. The time now is 04:48 AM.