Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing " to a file in bash

Tags:

bash

Simply I need to write

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

To a text file, including all the So the output would be something like:

echo "  t1  = $t1"

With " as they are. So I have 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 variations on this such as:

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

But I guess the wrapping in double " is only for variables.

Ideas?

like image 670
S1syphus Avatar asked Apr 20 '10 10:04

S1syphus


3 Answers

There's actually no wrapping in double quotes for variables, first quote starts, the second quote ends the quoted string. You may however concatenate strings however you want, so "$"t${count}"" will actually be equivalent to "$"t${count}, beginning with a quoted $-sign, followed by 't', followed by the count-variable.

Back to your question, to get a ", you can either put it in a literal string (surrounded by single quotes), like so echo '"my string"', note though that variables are not substituted in literal strings. Or you can escape it, like so echo "\"my string\"", which will put a literal " in the string, and won't terminate it.

like image 107
falstro Avatar answered Nov 15 '22 06:11

falstro


echo "echo \"t${count} = $t${count}\""
like image 28
ghostdog74 Avatar answered Nov 15 '22 06:11

ghostdog74


You need to escape the doublequote (also dollar sign):

echo "\" t = \$t\""
like image 20
Pasi Savolainen Avatar answered Nov 15 '22 06:11

Pasi Savolainen