1

I'm having an extremely hard time figuring out how to echo this:

![alt text](https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png "Logo Title Text 1")

I keep getting this error:

bash: ![alt: event not found

Using double quotes around it does not work. The using single quotes around it does work, however, I also need to echo strings that have single quotes within them. I wouldn't be able to wrap the string with single quotes then.

Is there a way to echo a string of ANY content?

Thanks.

EDIT: Here is some context. I am making a Markdown renderer that grabs the content of a code editor, then appends every line of the code individually into a text file. I am doing this by doing this:

echo TheLineOfMarkdown > textfile.txt
PuffySparrow
  • 897
  • 3
  • 10
  • 23
  • You can go with single-quotes and try this: http://stackoverflow.com/questions/1250079/escaping-single-quotes-within-single-quoted-strings – mcserep May 11 '14 at 22:36
  • where exactly does this string come from? please provide more context – Pavel May 11 '14 at 22:37

2 Answers2

2

Unlike in many programing languages, '...' and "..." in Bash do not represent "strings" per se; they quote/escape whatever they contain, but they do not create boundaries that separate arguments. So, for example, these two commands are equivalent:

echo foobar
echo "fo"ob'ar'

So if you need to quote some of an argument with single-quotes, and a different part of the argument has to contain single-quotes — no problem.

For example:

echo '![alt text](https://... "What'"'"'s up, Doc?")'

Another option is to use \, which is similar to '...' except that it only quotes a single character. It can even be used inside double-quotes:

echo "\![alt text](https://... \"What's up, Doc?\")"

For more information, see §3.1.2 "Quoting" in the Bash Reference Manual.

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • 1
    Ah, but the thing is, I don't know what I'll need to echo beforehand. I won't be able to add anything to the inside of the string, only something around the string. I've added an edit that provides some context on what I'm trying to accomplish. – PuffySparrow May 11 '14 at 23:09
  • @Regnarg: Please, *please* tell me that you are not injecting untrusted text into dynamically-generated Bash scripts for the sake of pasting it into a file. :-/ There must be a better, safer way. (For starters -- instead of injecting the text into a Bash script, why not just inject it directly into the intended file?) – ruakh May 11 '14 at 23:14
  • I'm not exactly sure if that's what I'm doing, but it kind of sounds like it. I thought it would be a simple task (it may still be), but I haven't been able to find an simple solution to it. I'm literally just trying to replace a file's content with a string that contains new line characters, but echo $string > file.txt did not work well. Any idea how I would do that via commandline? – PuffySparrow May 11 '14 at 23:27
  • @Regnarg: Wait, are you saying that this isn't in a shell script or anything, but rather, you're manually typing this at the command line? And the problem with complex quoting isn't that you *can't* properly quote a given string, but rather, that you're just looking for a way that doesn't involve as much manual effort? If so, then -- let me know, I can update my answer to show you an easier way. :-) – ruakh May 11 '14 at 23:35
  • 1
    Re: "a string that contains new line characters, but echo $string > file.txt did not work well": Right. You need `echo "$string" > file.txt` instead, to prevent word-splitting and filename expansion on the contents of `$string`. – ruakh May 11 '14 at 23:36
  • Arg, I'm getting so many different answers about what I'm supposed to do: http://stackoverflow.com/questions/23599061/how-to-replace-a-files-content-with-a-string-containing-new-line-characters – PuffySparrow May 11 '14 at 23:44
  • @Regnarg: Seems like lots of people have told you to use `echo "$string"` instead of `echo $string`, so the answers are not so different. – rici May 12 '14 at 00:04
  • @Regnarg: You're getting different answers because people are having trouble figuring out exactly what you're trying to do. ruakh's answer is correct *if the string is being entered as part of the command itself*. Your other question refers to a string *read from a file into a variable, then used* (and also needing `\n` translated to newline). If the string is given directly in the command line, you have to adapt your quoting to what characters it contains; if it's in a variable, double-quotes are (almost) always the right answer. – Gordon Davisson May 12 '14 at 04:38
1

! is annoying. My advice: Use \!.

! invokes history completion, which is also performed inside double-quotes. So you need to single-quote the exclamation mark, but as you say that conflicts with the need to not single-quote other single-quotes.

Remember that you can mix quotes:

$ echo '!'"'"'"'
!'"

(That's just one argument.) But in this case, the backslash is easier to type and quite possibly more readable.

rici
  • 234,347
  • 28
  • 237
  • 341
  • Ah, but the thing is, I don't know what I'll need to echo beforehand. I've added an edit that provides some context on what I'm trying to accomplish – PuffySparrow May 11 '14 at 22:55
  • @Regnarg: I don't get it. Are you programmatically generating the `echo foo >> textfile` lines? (Append is `>>`, `>` is overwrite). – rici May 12 '14 at 00:01