home / tils / til

Menu
  • GraphQL API

til: bash_escaping-a-string.md

This data as json

path topic title url body html shot created created_utc updated updated_utc shot_hash slug
bash_escaping-a-string.md bash Escaping strings in Bash using !:q https://github.com/simonw/til/blob/main/bash/escaping-a-string.md 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). <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> <Binary: 80,156 bytes> 2020-10-01T13:32:02-07:00 2020-10-01T20:32:02+00:00 2020-10-03T22:30:04-07:00 2020-10-04T05:30:04+00:00 7b7a5e24ac848f6bd511817576928928 escaping-a-string
Powered by Datasette · How this site works · Code of conduct