LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-20-2016, 11:55 AM   #1
Ambalo
LQ Newbie
 
Registered: Mar 2016
Posts: 19

Rep: Reputation: Disabled
Bash: save multiline text with variable in file


Hi,

i wanna save multiline text with variable in a file.

The following shows a example:

Code:
${text} = ${Array[i]}

${text[$p - $v]} =  ${Array[i]}
The red marked text and symbols should saved as symbol.
The green marked text and symbols should saved as the value.

e.i. saved text:
Code:
${text} = 8

${text[3 - 4]} =  8

I want save this text in ".txt" file

Code:
echo "'${text} ='  ${Array[i]} 
 '${text['$p '-' $v']} =' ${Array[i]}" > Test.txt
Unfortunately this doesn't work. What's wrong?
At the end I want implement this in a loop. For this reason I use "i" in the array.
 
Old 03-20-2016, 12:19 PM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
I'm assuming you're using bash with the requisite #!/bin/bash

Splitting it up into two echos should work:

Code:
echo '${text} ='  ${Array[i]} 
echo '${text['$p '-' $v']} =' ${Array[i]}
 
1 members found this post helpful.
Old 03-20-2016, 12:48 PM   #3
Ambalo
LQ Newbie
 
Registered: Mar 2016
Posts: 19

Original Poster
Rep: Reputation: Disabled
Thank you for respond. That works fine! A one-banana problem, huh?
 
Old 03-20-2016, 12:54 PM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by Ambalo View Post
Thank you for respond. That works fine! A one-banana problem, huh?
No problem, Ambalo. There are other ways of doing it without having to issue separate echo commands, but this does the trick without having to think about things too much (which is a way of working I like a lot).

If you could mark thread as "Solved", that would be great.
 
Old 03-20-2016, 01:56 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
@hydrurga - you are going to have to tell me which crystal ball you use as I had to re-read the question several times to finally understand why your solution was correct

@OP - my general thought when seeing multiline in bash is to use heredocs
 
Old 03-20-2016, 03:42 PM   #6
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by grail View Post
@hydrurga - you are going to have to tell me which crystal ball you use as I had to re-read the question several times to finally understand why your solution was correct

@OP - my general thought when seeing multiline in bash is to use heredocs
Now, I can't be giving away trade secrets, grail.

All I did was run OP's script on my machine, with a few variable assignments and a for loop added, and make it work.
 
  


Reply



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
extracting a variable's value from a text file using bash savio Programming 2 02-09-2016 11:01 AM
variable from text file bash drumbum Programming 1 07-30-2009 09:50 AM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
Getting a variable from a text-file (bash) PatrickBecks Programming 4 02-14-2008 08:52 PM
how to cat a text file and save it as a variable mrobertson Programming 37 07-05-2005 08:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:04 PM.

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