Skip to main content
Typos in several places.
Source Link
QuartzCristal
  • 2.1k
  • 5
  • 25

Quoting and using printf instead of echo might be the best habits.

First, your description doesn't match. If you have a variable with 'me \n you \n him \n' then it will not have a newline anywhere, not with echo $var, nor printf '%s' $var, nor with echo "$var" or printf '%s' "$var"

$ var='me \n you \n him \n'
$ echo $var
me \n you \n him \n

No newline, no ending newline (other than the one added by echo).

I believe that wahtwhat you have is something with newlines inside which you have represented by \n. Something like:

$ var=$'me \n you \n him \n'
$ printf '%s' "$var"
me 
 you 
 him
$

And here the quoting is (very) important:

$ printf '%s' $var
meyouhim$ 

.........

$ echo $var
meyouhim
$

.........

$ echo "$var"
echo "$var"
me 
 you 
 him 

$

.....

Note the additional newline introduced by echo? That is th eonly one removed with echo -n:

$ echo -n "$var"
echo "$var"
me 
 you 
 him 
$

That seems to be the problem with your file, it ends on two newline characters, one from the actual $Content var and one from echo.

If that is actually the problem, then, a echo -n "$var" > Names.txt would give you a file with only one ending newline.

Or much better, do:

$ Content=$'me \n you \n him \n' 
$ printf '%s' "$Content" > Names.txt
$ cat Names.txt 
me 
 you 
 him
$

Quoting and using printf instead of echo might be the best habits.

First, your description doesn't match. If you have a variable with 'me \n you \n him \n' then it will not have a newline anywhere, not with echo $var, nor printf '%s' $var, nor with echo "$var" or printf '%s' "$var"

$ var='me \n you \n him \n'
$ echo $var
me \n you \n him \n

No newline, no ending newline (other than the one added by echo).

I believe that waht you have is something with newlines inside which you have represented by \n. Something like:

$ var=$'me \n you \n him \n'
$ printf '%s' "$var"
me 
 you 
 him
$

And here the quoting is (very) important:

$ printf '%s' $var
meyouhim$ 

.........

$ echo $var
meyouhim
$

.........

$ echo "$var"
echo "$var"
me 
 you 
 him 

$

.....

Note the additional newline introduced by echo? That is th eonly one removed with echo -n:

$ echo -n "$var"
echo "$var"
me 
 you 
 him 
$

That seems to be the problem with your file, it ends on two newline characters, one from the actual $Content var and one from echo.

If that is actually the problem, then, a echo -n "$var" > Names.txt would give you a file with only one ending newline.

Or much better, do:

$ Content=$'me \n you \n him \n' 
$ printf '%s' "$Content" > Names.txt
$ cat Names.txt 
me 
 you 
 him
$

Quoting and using printf instead of echo might be the best habits.

First, your description doesn't match. If you have a variable with 'me \n you \n him \n' then it will not have a newline anywhere, not with echo $var, nor printf '%s' $var, nor with echo "$var" or printf '%s' "$var"

$ var='me \n you \n him \n'
$ echo $var
me \n you \n him \n

No newline, no ending newline (other than the one added by echo).

I believe that what you have is something with newlines inside which you have represented by \n. Something like:

$ var=$'me \n you \n him \n'
$ printf '%s' "$var"
me 
 you 
 him
$

And here the quoting is (very) important:

$ printf '%s' $var
meyouhim$ 

.........

$ echo $var
meyouhim
$

.........

$ echo "$var"
me 
 you 
 him 

$

.....

Note the additional newline introduced by echo? That is th eonly one removed with echo -n:

$ echo -n "$var"
me 
 you 
 him 
$

That seems to be the problem with your file, it ends on two newline characters, one from the actual $Content var and one from echo.

If that is actually the problem, then, a echo -n "$var" > Names.txt would give you a file with only one ending newline.

Or much better, do:

$ Content=$'me \n you \n him \n' 
$ printf '%s' "$Content" > Names.txt
$ cat Names.txt 
me 
 you 
 him
$
Source Link
QuartzCristal
  • 2.1k
  • 5
  • 25

Quoting and using printf instead of echo might be the best habits.

First, your description doesn't match. If you have a variable with 'me \n you \n him \n' then it will not have a newline anywhere, not with echo $var, nor printf '%s' $var, nor with echo "$var" or printf '%s' "$var"

$ var='me \n you \n him \n'
$ echo $var
me \n you \n him \n

No newline, no ending newline (other than the one added by echo).

I believe that waht you have is something with newlines inside which you have represented by \n. Something like:

$ var=$'me \n you \n him \n'
$ printf '%s' "$var"
me 
 you 
 him
$

And here the quoting is (very) important:

$ printf '%s' $var
meyouhim$ 

.........

$ echo $var
meyouhim
$

.........

$ echo "$var"
echo "$var"
me 
 you 
 him 

$

.....

Note the additional newline introduced by echo? That is th eonly one removed with echo -n:

$ echo -n "$var"
echo "$var"
me 
 you 
 him 
$

That seems to be the problem with your file, it ends on two newline characters, one from the actual $Content var and one from echo.

If that is actually the problem, then, a echo -n "$var" > Names.txt would give you a file with only one ending newline.

Or much better, do:

$ Content=$'me \n you \n him \n' 
$ printf '%s' "$Content" > Names.txt
$ cat Names.txt 
me 
 you 
 him
$