Writing to a file in bash

I just need to write

"echo" t${count} = "$"t${count}" 

To a text file, including everything. Thus, the output will look something like this:

 echo " t1 = $t1" 

With "as they are. Therefore, I tried:

 count=1 saveIFS="$IFS" IFS=$'\n' array=($(<TEST.txt)) IFS="$saveIFS" for i in "${array[@]}" do echo "echo" t${count} = "$"t${count}"" (( count++ )) done >> long1.txt 

And variants of this type:

 echo "echo" """"" t${count} = "$"t${count}"" 

But I think the double wrapper is for variables only.

Ideas?

+4
source share
4 answers

In fact, there is no double quote wrapping for variables, the first quote begins, the second quote ends with the quoted string. However, you can concatenate the lines, but you want to, so "$"t${count}"" will actually be equivalent to "$"t${count} , starting with the quoted $ -sign followed by 't' followed by variable count.

Let's go back to your question to get ", you can either put it in a literal string (surrounded by single quotes), for example, echo '"my string"' , note that these variables are not replaced by literal strings. Or you can avoid it like this echo "\"my string\"" , which will put the letter in the string and not complete it.

+7
source
 echo "echo \"t${count} = $t${count}\"" 
+3
source

You need to avoid double quotes (also a dollar sign):

 echo "\" t = \$t\"" 
+1
source

Another citation option (more likely an exercise than a solution, of course):

 # concatenate alternating single- & double-quoted blocks of substrings count=1 echo 'echo "t'"${count}"' = $t'"${count}"'"' 
+1
source

Source: https://habr.com/ru/post/1307443/


All Articles