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 |
Hi,
It's simple, you're missing " (double quotes) around the variable you're echo-ing Code:
#!/bin/bash |
Try putting it in a loop:
for filename in `ls -c1 *.help`; do echo "$filename" done |
Quote:
bash can read from the file system already. Code:
for f in *.help |
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
|
Quote:
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. |
Quote:
I was merely correcting Leo's line "echo `ls -c1 *.help` | tr "[:space:]" "\n"" to make echo print out with lines. |
Quote:
|
All times are GMT -5. The time now is 04:48 AM. |