0

I wrote this function in bash that basically loops each file I have in my main directory that ends with .m and then in each file reads line by line and takes from this line: place, name, points and print it.

My problem is that after echo, the function doesn't print a newline. I can't use echo -e which I saw many solutions like this but I can't find solutions without using it .

Here is an idea of what I am working with:

function print {  
for file in *.m; do  
   while read f ; do             
  local name="`get_name $num`"
  local points="`get_points $num`"
  local place="`echo "$f" | get_place $2`"
  echo "$file $name $points $place"
 done < "$file"
 done 
 }
  • 1
    What do you mean it doesn't print a newline? Do you want to print mulitple newlines? Why can't you use `-e`? Also printf is superior to echo in practically every situation so, so you may want to consider using that instead. – 123 Nov 10 '17 at 11:41
  • You can use `echo $'\r'` https://stackoverflow.com/questions/9741433/appending-a-line-break-to-an-output-file-in-a-shell-script –  Nov 10 '17 at 11:43
  • i want in each loop to print a new line after printing the name and pointes .. cause right now the function print everything in the same line .. i can't use -e as requisted from our prof .. also the prof saied he prefare us to use only echo ! i don't know why – newprogrammerha Nov 10 '17 at 11:54
  • i tried "echo $'\r'" and it also not working :/ – newprogrammerha Nov 10 '17 at 11:56
  • 1
    There's something wrong with your TTY, or your terminal emulator, or some other external factor. `echo "$file $name $points $place"` absolutely prints a newline. – John Kugelman Nov 10 '17 at 12:06
  • yeah it worked finally the problem was in the way i called the function – newprogrammerha Nov 10 '17 at 13:16
  • Ignore the `function` keyword; just use POSIX-style function declarations: `print () { ... }`. – chepner Nov 10 '17 at 14:08

1 Answers1

2

You can use this instead of echo: printf "\n"

robert
  • 3,539
  • 3
  • 35
  • 56
  • Just to be sure I also tried and it works. GNU bash, version 3.2.53(1)-release (x86_64-apple-darwin13) – robert Nov 10 '17 at 11:52