til: bash_escaping-a-string.md
- path
-
bash_escaping-a-string.md
- topic
-
bash
- title
-
Escaping strings in Bash using !:q
- body
-
TIL this trick, [via Pascal Hirsch](https://twitter.com/phphys/status/1311727268398465029) on Twitter. Enter a line of Bash starting with a `#` comment, then run `!:q` on the next line to see what that would be with proper Bash escaping applied. ``` bash-3.2$ # This string 'has single' "and double" quotes and a $ bash-3.2$ !:q '# This string '\''has single'\'' "and double" quotes and a $' bash: # This string 'has single' "and double" quotes and a $: command not found ``` How does this work? [James Coglan explains](https://twitter.com/mountain_ghosts/status/1311767073933099010): > The `!` character begins a history expansion; `!string` produces the last command beginning with `string`, and `:q` is a modifier that quotes the result; so I'm guessing this is equivalent to `!string` where `string` is `""`, so it produces the most recent command, just like `!!` does A bunch more useful tips in the [thread about this on Hacker News](https://news.ycombinator.com/item?id=24659282).
- html
-
<p>TIL this trick, <a href="https://twitter.com/phphys/status/1311727268398465029" rel="nofollow">via Pascal Hirsch</a> on Twitter. Enter a line of Bash starting with a <code>#</code> comment, then run <code>!:q</code> on the next line to see what that would be with proper Bash escaping applied.</p> <pre><code>bash-3.2$ # This string 'has single' "and double" quotes and a $ bash-3.2$ !:q '# This string '\''has single'\'' "and double" quotes and a $' bash: # This string 'has single' "and double" quotes and a $: command not found </code></pre> <p>How does this work? <a href="https://twitter.com/mountain_ghosts/status/1311767073933099010" rel="nofollow">James Coglan explains</a>:</p> <blockquote> <p>The <code>!</code> character begins a history expansion; <code>!string</code> produces the last command beginning with <code>string</code>, and <code>:q</code> is a modifier that quotes the result; so I'm guessing this is equivalent to <code>!string</code> where <code>string</code> is <code>""</code>, so it produces the most recent command, just like <code>!!</code> does</p> </blockquote> <p>A bunch more useful tips in the <a href="https://news.ycombinator.com/item?id=24659282" rel="nofollow">thread about this on Hacker News</a>.</p>
- created
-
2020-10-01T13:32:02-07:00
-
created_
utc -
2020-10-01T20:32:02+00:00
- updated
-
2020-10-03T22:30:04-07:00
-
updated_
utc -
2020-10-04T05:30:04+00:00
-
shot_
hash -
7b7a5e24ac848f6bd511817576928928
- slug
-
escaping-a-string