Making shapes with linux shell script

2019-09-15 03:41发布

问题:

For an assignment, I'm trying to make a shell script that will print a triangle that looks like the following:

+
| \
|  \
|   \
|    \
+-----

Here is my code in VIM:

echo'+
     | \
     |  \
     |   \
     |    \
     +----- '

However, instead of getting that as the output when I run the script, it outputs as the following:

Can anybody tell me what I'm doing wrong?

回答1:

Try this

#!/bin/bash
echo '
     +
     | \
     |  \
     |   \
     |    \
     +----- '

just start it on the next line since you need spaces before the "+"



回答2:

How did your output got merged to 3 lines?
I think your original command was with a space after echo and double quotes:

echo "+
     | \
     |  \
     |   \
     |    \
     +----- "

And now pay attention to the last character of each line. When the last character is the \, the following line is appended to the current line.
Make sure each line ends with a space (or use single quotes).



标签: linux shell