This question already has answers here:
Echo newline in Bash prints literal \n
(22 answers)
Closed 1 year ago.
A very simple question. I'd like to write several lines to a file with just one line containing multiple \n. I.e. my input string would be hello\nhello\nhello, I'd like to write it to a file hello.txt, and when cat hello.txt it should give me
hello
hello
hello
However, when I do echo "hello\nhello\nhello" > hello.txt, it just gave me the literal string, without converting \n to a newline.
I wonder how to write such a line to a file.
You want either echo -e or, generally even better printf
printf 'Hello %s\n\nWelcome to my %s document\nmultiline document\n\n\n\nEnd\n' \
"John" "yellow" > "/some/path/hello.txt";
Related
This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 1 year ago.
How to save a line with spaces without line breaks in a file?
Such as this:
Value2="Server=server1.windows.net,1433;Database=DB;Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
using:
printf '%s\n' $Value2 >> "./print.csv"
At present it gets saved as:
Server=server1.windows.net,1433;Database=DB;Persist
Security
Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection
Timeout=30;
The variable $Value2 isn't quoted, and has two separate spaces, so the printf command sees three strings, not one -- so printf prints three strings on three lines. To make printf see just one string, put the variable in quotes, like this:
printf '%s\n' "${Value2}"
Output:
Server=server1.windows.net,1433;Database=DB;Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
Or, as proof use wc to count the lines:
printf '%s\n' "${Value2}" | wc -l
Output:
1
This question already has answers here:
How can I have a newline in a string in sh?
(13 answers)
Closed 3 years ago.
I want to create a variable that contains a string of text [in BASH] that spans multiple lines and includes spaces. I can do this without spaces, but not with spaces.
I am trying to create a flash card program for self study. I am not in school and this is not homework.
This works:
$ TEST=me$'\n'you
$ echo "$TEST"
me
you
This does not work:
$ TEST="me on line one$'\n'you on line two"
$ echo "$TEST"
me on line one$'\n'you on line two
I would like the output of the second code to look like this:
me on line one
you on line two
Note: this is not a duplicate question. The alleged duplicate does not contain an example with spaces, which is why I had a problem.
$ test=$'me on line one\nyou on line two'
$ echo "$test"
me on line one
you on line two
If you insist on using different quoting methods for different parts of the string (why?), you need something like
$ test="me on line one"$'\n'"you on line two"
This question already has answers here:
How to append output to the end of a text file
(13 answers)
Closed 5 years ago.
If I'm in terminal and I write some basic string to a file, is there a way to continually append to that file using echo?
For instance, echo 'hello' > file will put 'hello' into that file, but what if I now want to append ' world' to file? I know that if I do echo ' world', it'll overwrite the first string I wrote into file. Is there any += operator I can use in bash?
EDIT: Nevermind, I can append using >>. Is it possible to append to the same line instead of to a new line?
echo -n 'Hello' > file
echo ', World!' >> file
The >> redirection operator means "append."
Here we're using a non-standard extension of echo, where -n tells it not to append a new line.
If you want standard-compliant behavior, use printf:
printf 'Hello' > file
printf ', World!\n' >> file
Edit: using double quote around a string containing the exclamation ! may not work with some versions. It could get interpreted as a history expansion token. Use single quotes to prevent this.
This question already has answers here:
Losing newline after assigning grep result to a shell variable
(4 answers)
Closed 7 years ago.
I have a program that is trying to print multiple lines from a text document. I have my main bash program, and I am using the line:
Multlines=`</Users/$USER/Documents/text.txt`
echo $Multlines
Where text.txt may look something like
John
Smith
but the echo prints John Smith where I want it to print it on different lines.
How do I print it out that way?
To preserve the newline characters in the variable when printing with echo, you need to double quote it:
echo "$Multlines"
You could also just use cat instead of echo, assuming you had it available to you.
This question already has answers here:
Bash and filenames with spaces
(6 answers)
Closed 8 years ago.
I'm writing a script to do variable substitution into a Java properties file, of the format name=value. I have a source file, source.env like this:
TEST_ENV_1=test environment variable one
TEST_ENV_2=http://test.environment.com/one
#this is a comment with an equal sign=blah
TEST_ENV_3=/var/log/test/env/2.log
My script will replace every occurence of TEST_ENV_1 in the file dest.env with "test environment variable one", and so on.
I'm trying to process a line at a time, and having problems because looping on output from a command like sed or grep tokenizes on white space rather than the entire line:
$ for i in `sed '/^ *#/d;s/#.*//' source.env`; do
echo $i
done
TEST_ENV_1=test
environment
variable
one
TEST_ENV_2=http://test.environment.com/one
TEST_ENV_3=/var/log/test/env/2.log
How do I treat them as lines? What I want to be able to do is split each line apart on the "=" sign and make a sed script with a bunch of substitution regex's based on the source.env file.
sed '/^ *#/d;s/#.*//' source.env | while read LINE; do
echo "$LINE"
done
An alternative is to change $IFS as per #Jim's answer. It's better to avoid backticks in this case as they'll cause the entire file to be read in at once, whereas piping the output of sed to while above will allow the file to be processed line by line without reading the whole thing in to memory.