[QUESTION] echoing a variable

Hi...I am trying to make a script like this:

mmc=123
echo "$mmc" > 123.txt

The variable "mmc" has to be declared right on the beginning of the script, so when I open 123.txt, I get:

123

My question is, how can I "echo" '$mmc' into 123.txt, retaining the '$mmc' phrase? Which means when I open 123.txt, I will get:

$mmc

Thanks in advance...:slight_smile:

Ah, use single quotes. '$mmc' and it will not substitute.

I don't know why you'd want that, as $mmc in a text file isn't really useful. It will be very difficult to turn it back into a variable later.

Is this what you are trying to achieve:-

echo "\$mmc"

Cause I am actually trying to tell the script to create another script...:stuck_out_tongue:

I was afraid of that. If you think you need to generate a script to do something shell can't do, you're probably missing a few important things. Last time, it was a fellow who wanted to generate script0001.sh-script9999.sh to work on file0001 through file9999, because he didn't know how to use loops and commandline parameters...

Why not make a script that just does what you want in the first place? What feature are you missing which would allow you to do so?

Actually I am trying to write a shell script for android phones...so the script was supposed to generate another script in the init.d folder so that it will run on boot...the main script can only be run manually...:slight_smile:

One thing about looping...I tried to use while loops with nested if...but I got "while" unmatched errors all the time...checked the parameters and they are correct, and if I remove the nested ifs, then it will loop...weird...

You may find here-documents useful:

cat >filenane <<"EOF"
some text
more text
and stuff
EOF

Note the ending EOF must be at the beginning of the line, not indented.

Without seeing your code, I cannot guess what you did wrong with your nested loops.

What i the reason for you to double quote EOF , it works fine without qote?

@Jotne:
with quotes, the shell will not touch whatever is inside the heredoc.

E.g.,

$ name='JOTNE'

$ cat <<EOF
> Name is $name
> EOF

Name is JOTNE

$ cat <<"EOF"
> Name is $name
> EOF

Name is $name

Thanks.
Another discover I find, is the use of - before EOF .
It suppress tabs and spaces in front of line to be show on output. Makes programming more struktured.

$ name='JOTNE'

$ cat <<EOF
> 	Name is $name
> EOF

	Name is JOTNE


$ cat <<-EOF
> 	Name is $name
> EOF

Name is JOTNE

As far as I know, tabs and only tabs.