Eric
Eric

Reputation: 409

Echo in Bash: Escape Apostrophes?

I'm trying to use echo in bash inside of quotes. When I try from a command line, it works fine.

For example: echo "I'm testing the apostrophe functionality." yields I'm testing the apostrophe functionality.

Yet, when I write this in a script, it doesn't seem to work.

Here's a snippet of my code: (I'm trying to integrate ASCII art into my program)

if [ "$2" == "-s" ]

  then echo "   ___                           __ _             _            "
  echo "  / _ \__ _ _ __ ___   ___      / _\ |_ __ _ _ __| |_ ___ _ __ "
  echo " / /_\/ _` | '_ ` _ \ / _ \_____\ \| __/ _` | '__| __/ _ \ '__|"
  echo "/ /_\\ (_| | | | | | |  __/_____|\ \ || (_| | |  | ||  __/ |   "
  echo "\____/\__,_|_| |_| |_|\___|     \__/\__\__,_|_|   \__\___|_|   "
  echo ""                                                              
  echo "Hello!  My name is Siri."
  echo "I'm not actually the Siri you're probably used to."
  echo "I'm actually Apple's Siri's sister, the no-voice one."
  echo "Sorry, but I'm in development right now."
  echo "Come back later and maybe Eric will bring me out of beta."
  echo "Thanks for reading this long debug message!"
fi

I've checked and double-checked all my quotes...

Yet it still yields:

./game-starter.sh: line 7: unexpected EOF while looking for matching ``'
./game-starter.sh: line 88: syntax error: unexpected end of file

Please help soon!

-HewwoCraziness

Upvotes: 2

Views: 1388

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74615

As you're using double quotes around your strings, certain characters are interpreted by the shell. One example is the backtick, as mentioned in Ryan's answer.

One option would be to use single quotes around your strings, although then you would have to escape the apostrophes in your message. I think that the best solution would be to use a heredoc instead:

cat <<'EOF'
   ___                           __ _             _            
  / _ \__ _ _ __ ___   ___      / _\ |_ __ _ _ __| |_ ___ _ __ 
 / /_\/ _` | '_ ` _ \ / _ \_____\ \| __/ _` | '__| __/ _ \ '__|
/ /_\\ (_| | | | | | |  __/_____|\ \ || (_| | |  | ||  __/ |   
\____/\__,_|_| |_| |_|\___|     \__/\__\__,_|_|   \__\___|_|   

Hello!  My name is Siri.
I'm not actually the Siri you're probably used to.
I'm actually Apple's Siri's sister, the no-voice one.
Sorry, but I'm in development right now.
Come back later and maybe Eric will bring me out of beta.
Thanks for reading this long debug message!
EOF

The quotes around the EOF mean that the string is interpreted literally, so characters such as | don't cause problems.

Upvotes: 2

Ryan Vickers
Ryan Vickers

Reputation: 79

I don't think it's the apostrophes that are causing your issue; is it the ` character (you know, on the ~ key). It is used for running commands in place and other things, and is probably what's causing the issue, if I had to guess based on that error message.

Upvotes: 0

Related Questions