Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi all,
I haven't found an answer to this just yet, and maybe one of you can help. I need to have a script write multiple lines of text to a file. Instead of doing it like this:
echo stuff > /etc/file
echo more stuff >> /etc/file
Is there a way to make it so that I don't have to prefix every line with "echo" and put the path after each line? Maybe there is a better way to do this?
Thanks,
Pair
Click here to see the post LQ members have rated as the most helpful post in this thread.
Just start it with a quote and don't use the closing quote until you are done. For example....
Code:
# echo " this is line one
> this is line two
> this is line thre
> rats, spelling is off" > file.txt
# cat file.txt
this is line one
this is line two
this is line thre
rats, spelling is off
Just start it with a quote and don't use the closing quote until you are done. For example....
Code:
# echo " this is line one
> this is line two
> this is line thre
> rats, spelling is off" > file.txt
# cat file.txt
this is line one
this is line two
this is line thre
rats, spelling is off
I'm trying to create a file by echoing as well but my file is a script and starts with #!/bin/sh and I'm having problems with that. This is what I get...
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Use for that line a single quote. Double quotes are weak quotes and special characters are still recognized, '#!' apparently being on eof them. Single quotes are strong: what is between the quotes is takens as string.
The "DELIM" will not be written to the file. This method is known as a "here document". It allows for variable expansion as well, and is a common method used to do just what you said you wanted to do.
It allows you to embed documents you want written inside a single script. It was once a common technique used in installation scripts.
Quote:
$ echo "#!/bin/sh
bash: !/bin/sh: event not found
In an interactive shell, the exclamation point is expanded to a previous bash command. This isn't the case with non-interactive scripts however.
Please refer to the "info bash" manual for the details. Here documents are covered in Section 3.6.5. The Event designator (!) is covered in section 9.3.1.
I'm running this moment on a windows box equipped with MinGW32:
Code:
$ cat test.sh
#/bin/sh
cat > ./outfile <<DELIM
hello proble
h2 fadernaly
DELIM
echo "I just wrote 2 lines to the file ./outfile"
mustafa@LILPC ~
$ ./test.sh
I just wrote 2 lines to the file ./outfile
mustafa@LILPC ~
$ cat ./outfile
hello proble
h2 fadernaly
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.