LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2021, 01:26 AM   #1
Varister
LQ Newbie
 
Registered: Oct 2021
Posts: 5

Rep: Reputation: Disabled
How can I store multiple lines into a temporary file?


I am trying to store ASCII art into a temporary file using > $temp in BASH for my dialog menu, but when I put it at the end of my art, only the line containing > $temp is displayed and the rest of my art is missing. I have tried various ways including putting > $temp at the end of each line, putting > $temp at the beginning and end of the art, but nothing works.

I have used echo on each line of my ASCII art.

For example, if I did:

echo 'a'
echo 'b'
echo 'c' > $temp

only the c would appear in my dialog menu and the rest of the text is missing.
 
Old 10-30-2021, 01:32 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,224
Blog Entries: 1

Rep: Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585
Two options from the many:
Code:
(
 echo 'a b   b a'
 echo ' a b b a '
) >myfile.txt
Code:
 echo 'a b   b a' >>myfile.txt
 echo ' a b b a ' >>myfile.txt
 
Old 10-30-2021, 01:40 AM   #3
Varister
LQ Newbie
 
Registered: Oct 2021
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thank you, this worked!

Quote:
Originally Posted by NevemTeve View Post
Two options from the many:
Code:
(
 echo 'a b   b a'
 echo ' a b b a '
) >myfile.txt
Code:
 echo 'a b   b a' >>myfile.txt
 echo ' a b b a ' >>myfile.txt
 
Old 10-30-2021, 03:06 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,971
Blog Entries: 3

Rep: Reputation: 3102Reputation: 3102Reputation: 3102Reputation: 3102Reputation: 3102Reputation: 3102Reputation: 3102Reputation: 3102Reputation: 3102Reputation: 3102Reputation: 3102
Another way would be with a Here document:

Code:
$ cat << EOT > temp.txt
> a
> b
> c
> EOT

$ cat temp.txt 
a
b
c
 
Old 10-31-2021, 09:19 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,856

Rep: Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827
Round brackets enforce a sub shell.
Curly brackets make the shell avoid a sub shell:
Code:
{
 echo 'a b   b a'
 echo ' a b b a '
} >myfile.txt
In this case the effect is the same.

But an assignment makes a difference:
Code:
{
 echo 'a b   b a'
 var1="test"
 echo ' a b b a '
} >myfile.txt
echo "$var1"
This won't work if the assignment happened in a sub shell.
The sub shell is a fork(copy) of the main shell, and assignments (and other settings) are not copied back to the main shell.

A string can be multi-line.
If there is nothing but echo commands then you can do one echo
Code:
echo 'a b   b a
 a b b a ' >myfile.txt

Last edited by MadeInGermany; 10-31-2021 at 09:23 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How To Empty a File, Delete N Lines From a File, Remove Matching String From a File, And Remove Empty/Blank Lines From a File In Linux LXer Syndicated Linux News 0 11-22-2017 01:30 PM
[SOLVED] CAT command | multiple lines to multiple lines udiubu Programming 11 10-28-2011 07:09 AM
copy last 10000+ lines of large text file to a temporary file emilyg Linux - Newbie 3 06-24-2009 03:43 PM
best way to store copies of temporary C-structs for later lookup ta0kira Programming 12 02-19-2008 09:15 AM
Setup is unable to find a hard disk location to store temporary files. Klas Linux - Software 2 02-14-2006 07:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration