How would you include newlines in a C-shell echo command?

It sounds ridiculously easy, and it is with other sinks. But I can't figure out how to get the echo to display new lines. For example -

cat myFile

      

shows the file as it really is, what I want -

this
is
my
file

      

whereas my script which contains the following -

#!/bin/csh
set var = `cat myFile`
echo "$var"

      

deletes all new lines, which is not what I want -

this is my file

      

Thanks in advance.

+3


source to share


2 answers


The problem is not the command echo

, but the csh handling. When you do

set var = `cat myFile`

      

newlines from are myfile

never saved to $var

; they are converted to spaces. I can't think of a way to get the csh variable to include newlines read from the file, although there might be a way to do it.

sh and its derivatives behave the way you want. For example:

$ x="`printf 'foo\nbar'`"
$ echo $x
foo bar
$ echo "$x"
foo
bar
$ 

      



Double quotes in the assignment force newlines to be stored (except the last one). echo $x

replaces newlines with spaces, but echo "$x"

preserves them.

Your best bet is to do something other than trying to store the contents of the file in a variable. You said in a comment that you are trying to send an email with the contents of a log file. So download the contents of the file directly to whatever mail command you use. I don't have all the details, but it might look something like this:

( echo this ; echo that ; echo the-other ; cat myFile ) | some-mail-command

      

Mandatory link: http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

+6


source


You can use Awk to delimit each line with "\ n" before the shell concatenates them.

set var = `cat myfile | awk '{printf("%s\\n", $0)'}`

      



Assuming your echo command interprets "\ n" as a newline, echo ${var}

should play it cat myfile

without the need for additional file access. If the newline code is not recognized, you can try adding the -e flag to echo and / or use / bin / echo.

+1


source







All Articles