'Can't get ASCII art to echo to console
I'm new to Bash scripting, and this here is just puzzling to me. I'm adding ASCII art to a project, and can't seem to figure out how to escape certain characters. Would someone please help me get the following code below to work?
Whenever I tried adding slashes as escape characters to fix the errors, the slashes also wound up printing to console on execution. This ruins the image. I don't understand what I'm doing wrong, so I've posted the code below in the hopes that someone will take a moment to show me the right way. Please? I've removed the quotes to prevent more clutter.
echo -en "\E[31m"
echo
echo _,.
echo ,` -.)
echo '( _/'-\\-.
echo /,|`--._,-^| ,
echo \_| |`-._/|| ,'|
echo | `-, / | / /
echo | || | / /
echo `r-._||/ __ / /
echo __,-<_ )`-/ `./ /
echo ' \ `---' \ / /
echo | |./ /
echo / // /
echo \_/' \ |/ /
echo | | _,^-'/ /
echo | , `` (\/ /_
echo \,.->._ \X-=/^
echo ( / `-._//^`
echo `Y-.____(__}
echo | {__)
echo ()`
Solution 1:[1]
echo takes a series of arguments. If you type
echo foo bar
the echo command gets two arguments, "foo" and "bar", and the spacing between the words is discarded.
For what you're trying to do, you probably want echo to receive exactly one argument for each line. In bash, the easiest way is probably to use so-called "ANSI-C Quoting". Within each string, each apostrophe ' and backslash \ character has to be escaped with a backslash.
Here's a version of your script using this method:
#!/bin/bash
echo -n $'\E[31m'
echo $''
echo $' _,.'
echo $' ,` -.)'
echo $' \'( _/\'-\\\\-.'
echo $' /,|`--._,-^| ,'
echo $' \\_| |`-._/|| ,\'|'
echo $' | `-, / | / /'
echo $' | || | / /'
echo $' `r-._||/ __ / /'
echo $' __,-<_ )`-/ `./ /'
echo $'\' \\ `---\' \\ / /'
echo $' | |./ /'
echo $' / // /'
echo $'\\_/\' \\ |/ /'
echo $' | | _,^-\'/ /'
echo $' | , `` (\\/ /_'
echo $' \\,.->._ \\X-=/^'
echo $' ( / `-._//^`'
echo $' `Y-.____(__}'
echo $' | {__)'
echo $' ()`'
(The added backslashes do mess up the picture in the script, but it appears correctly on output.)
For this case, that other guy's answer is a better approach, since it avoids the need to escape any of the special characters, but this technique could be useful for smaller output.
Or you could just put the raw picture into a file and cat it to standard output.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
