0

I want to write a full shell script using >> to append commands to a file. For example, consider the following script as if it was created with an editor.

echo Hello World, done
echo please enter the first number
read num1
echo please enter the second number
read num2
echo $num1 plus $num2 is `expr $num1 + $num2`

In a terminal window, I have tried typing echo echo Hello World, "done" >> file.sh to write all the lines to a file. I put quotation marks around "done" to make sure that when I run the file, the terminal doesn't consider the keyword "done". However, when I check the file to see if I have done it correctly, the keyword "done" doesn't have the quotation marks. Also the last line in the code above, when typed into a terminal window and appended into the file, all the file displays is echo plus is. I am wondering why the variables and the expr expression don't append to the file. I basically want to recreate the code above from a terminal window. I am running Ubuntu 12.04 with bash version 4.2.24

2
2

tldr; the shell is interpreting the characters before appending to the file.

echo echo Hello World, "done" >> file.sh

The shell interprets the ", removing them from the standard out of echo. Hence, you need to either escape them,

echo echo Hello World, \"done\" >> file.sh

or, more simply, enclose the whole thing in '.

echo 'echo Hello World, "done"' >> file.sh`

Having said that, I'm not sure what you mean by

to make sure that when I run the file, the terminal doesn't consider the keyword "done"

A done after an echo means nothing. The new script will just print a literal done. Just test it!

Similarly, for

echo $num1 plus $num2 is `expr $num1 + $num2`

the shell interprets the variables and expr before it appends to the file. Again, just enclose everything in '.

echo 'echo $num1 plus $num2 is `expr $num1 + $num2`' >> file.sh

Having said that, I have no idea why you'd want to do this. Just use a text editor. It's going to be fiddly when you want literal 's in your script.

1
  • that worked! I never knew the ' (single quote) could be used like that. I am doing this because I want to challenge myself and not use text editors. Also thank you for reminding me how to escape characters. Also, I didn't want bash to "use/recognize" the keyword "done" or any unintended keywords when executing the file. Nov 25 '18 at 6:11
1

Try the following structure:

$ cat > file.sh << "EOF"
echo Hello World, done
read num1
echo please enter the second number
read num2
echo Sum of $num1 and $num2 is `expr $num1 + $num2`
EOF

Linux from Scratch uses this method for everthing from scripting to file creation. If the done overrides the EOF, consider escaping it as Sparhawk suggests.

3
  • Thank you for the suggestion, I still have to learn << and EOF. I'm fairly new to shell scripting. Nov 25 '18 at 6:13
  • @programmerjc6 In this context, this is shorthand for Open a here document saved as file.sh and cat the contents until the shell reads EOF EOF is short for End of File You can actually replace EOF with anything you like as it's a string, nothing special. Link: Here Document. Look at example 19.3
    – eyoung100
    Nov 25 '18 at 6:38
  • @programmerjc6 No problem. That's why the contributors write our answers. You might want to watch the tag shell-script.
    – eyoung100
    Nov 25 '18 at 6:41
1

The easiest way to create a text file (which is what a shell script is) from an interactive shell in a terminal using redirections is simply to do

$ cat >filename

... and then paste whatever text you want or start typing (use >> instead of > to append to an already existing file). When done, press Ctrl+D.

As an extra bonus, the shell will additionally not interpret the things that you paste or type into the text file this way. Were you to use echo to insert individual lines or a here-document to insert multiple lines, you would have to remember to protect the text through proper use of quotes to prevent the shell from expanding variables, escape sequences etc. in the contents of the document.

To create a script from within a script, use a quoted here-document

cat <<'END_SCRIPT' >filename
script
goes
here
END_SCRIPT

The issue that you are experiencing in your question is that when simply prepending echo to lines, the rest of the line is still interpreted by the shell, unless you take extra precautions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.