TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Escaping strings in Bash using !:q

482 pointsby goranmoominover 4 years ago

18 comments

kbensonover 4 years ago
If only more languages supported quote operators like Perl. They really do make certain things like this so much easier.<p>Non interpolating quotes:<p><pre><code> q&#x2F;I wasn&#x27;t surprised when he said &quot;boo&quot; and game me $5&#x2F;; q@I wasn&#x27;t surprised when he said &quot;boo&quot; and game me $5@; q!I wasn&#x27;t surprised when he said &quot;boo&quot; and game me $5!; q(I wasn&#x27;t surprised when he said &quot;boo&quot; and game me $5); q[I wasn&#x27;t surprised when he said &quot;boo&quot; and game me $5]; </code></pre> Interpolating quotes:<p><pre><code> qq&#x2F;Hello, $name!&#x2F;; qq!Hello, $name\!!; qq@Hello, $name!@; qq(Hello, $name!); qq[Hello, $name!]; qq&#x27;Hello, $name!&#x27;; </code></pre> Rule of thumb, single q for single quote string (non-interpolating normally), and two q&#x27;s for a double quote string (normally interpolates).
评论 #24660397 未加载
评论 #24660768 未加载
评论 #24661044 未加载
评论 #24667981 未加载
评论 #24660659 未加载
评论 #24660516 未加载
评论 #24660420 未加载
评论 #24660743 未加载
评论 #24660457 未加载
评论 #24663254 未加载
评论 #24661053 未加载
评论 #24665301 未加载
anvandareover 4 years ago
Cool trick!<p>Adding an additional :p modifier prevents the complaint about command not found:<p><pre><code> $ # This string &#x27;has single&#x27; &quot;and double&quot; quotes and a $ $ !:q &#x27;# This string &#x27;\&#x27;&#x27;has single&#x27;\&#x27;&#x27; &quot;and double&quot; quotes and a $&#x27; # This string &#x27;has single&#x27; &quot;and double&quot; quotes and a $: command not found $ $ # This string &#x27;has single&#x27; &quot;and double&quot; quotes and a $ $ !:q:p &#x27;# This string &#x27;\&#x27;&#x27;has single&#x27;\&#x27;&#x27; &quot;and double&quot; quotes and a $&#x27; $</code></pre>
评论 #24661747 未加载
cturover 4 years ago
Even better -- use this script, paste anything into stdin. It will come back out quotable and pasteable into a shell.<p><pre><code> #!&#x2F;bin&#x2F;bash printf &#x27;%q&#x27; &quot;$(cat)&quot; echo</code></pre>
评论 #24659808 未加载
评论 #24660022 未加载
评论 #24664482 未加载
评论 #24660493 未加载
yonrgover 4 years ago
I learned recently that escaping characters in zsh&#x2F;bash also works for parameter expansion:<p><pre><code> # zsh using flags ${(flags)name} % string=&quot;This is a string with \&quot;\&quot;\&quot; and &#x27;&#x27;&#x27; and \&quot;\&quot; again &#x27;&#x27;. Also such stuff as &amp; % # ;&quot; % echo $string This is a string with &quot;&quot;&quot; and &#x27;&#x27;&#x27; and &quot;&quot; again &#x27;&#x27;. Also such stuff as &amp; % # ; % echo ${(q)string} This\ is\ a\ string\ with\ \&quot;\&quot;\&quot;\ and\ \&#x27;\&#x27;\&#x27;\ and\ \&quot;\&quot;\ again\ \&#x27;\&#x27;.\ Also\ such\ stuff\ as\ \&amp;\ %\ \#\ \; # bash using operators ${name@operator} % string=&quot;This is a string with \&quot;\&quot;\&quot; and &#x27;&#x27;&#x27; and \&quot;\&quot; again &#x27;&#x27;. Also such stuff as &amp; % # ;&quot; % echo $string This is a string with &quot;&quot;&quot; and &#x27;&#x27;&#x27; and &quot;&quot; again &#x27;&#x27;. Also such stuff as &amp; % # ; % echo ${string@Q} &#x27;This is a string with &quot;&quot;&quot; and &#x27;\&#x27;&#x27;&#x27;\&#x27;&#x27;&#x27;\&#x27;&#x27; and &quot;&quot; again &#x27;\&#x27;&#x27;&#x27;\&#x27;&#x27;. Also such stuff as &amp; % # ;&#x27; </code></pre> Wow, so easy then, I remember struggling so many times in the past when e.g. iterating over filenames with unusual cahracters.<p>&#x2F;edit: fix markup
评论 #24667265 未加载
k_bxover 4 years ago
One thing I&#x27;ve figured is that in bash you can use $&#x27;These kinds of strings&#x27;, without any variable expansion, but what you get is essentially what&#x27;s present in most programming languages quote-wise. Example:<p><pre><code> $ echo $&#x27;hey there, it\&#x27;s &quot;double quotes&quot;, \&#x27;single quotes\&#x27;, and some \\, \&#x27;, &quot;, $ chars&#x27; hey there, it&#x27;s &quot;double quotes&quot;, &#x27;single quotes&#x27;, and some \, &#x27;, &quot;, $ chars</code></pre>
评论 #24661580 未加载
jribover 4 years ago
zsh has quote-line by default bound to alt-&#x27; which will escape your current command line:<p><pre><code> quote-line (ESC-’) (unbound) (unbound) Quote the current line; that is, put a ‘’’ character at the beginning and the end, and convert all ‘’’ characters to ‘’\’’’. </code></pre> <a href="http:&#x2F;&#x2F;zsh.sourceforge.net&#x2F;Doc&#x2F;Release&#x2F;Zsh-Line-Editor.html" rel="nofollow">http:&#x2F;&#x2F;zsh.sourceforge.net&#x2F;Doc&#x2F;Release&#x2F;Zsh-Line-Editor.html</a>
评论 #24662496 未加载
emmelaichover 4 years ago
Note that you can precede the !:q with `echo` to see the result as well as the invocation without an error.<p><pre><code> $ echo !:q echo &#x27;# This string &#x27;\&#x27;&#x27;has single&#x27;\&#x27;&#x27; &quot;and double&quot; quotes and a $&#x27; # This string &#x27;has single&#x27; &quot;and double&quot; quotes and a $</code></pre>
评论 #24661333 未加载
评论 #24665907 未加载
ladbergover 4 years ago
Is anyone else annoyed by the amount of built in magic character strings in bash? There are times where I know a task is possible in a bash script but make it in Python (with no dependencies required and compatible with 2 and 3) because it&#x27;s easier than looking up every random gotcha and running into issues later on when someone runs it in a directory with a space.
评论 #24669922 未加载
twicover 4 years ago
I turned off histexpand because i was fed up with exclamation marks doing random things i never wanted, so this doesn&#x27;t work for me :(<p>Still, i can use ctur&#x27;s script approach.
bachmeierover 4 years ago
You can always take advantage of what I believe is the portable property of consecutive strings (without space) being concatenated together. Then you never need to escape anything in your scripts.<p>For instance, to produce a double quote inside single quotes, you can do this<p>echo &quot;&#x27;&quot;&#x27;&quot;&#x27;&quot;&#x27;&quot;<p>That&#x27;s three quoted strings next to each other that produce<p>&#x27;&quot;&#x27;
评论 #24661589 未加载
w0mbatover 4 years ago
I thought &quot;!:q &quot; was what you typed to quit vi.
评论 #24664214 未加载
jfhuflover 4 years ago
This works too:<p><pre><code> % cat &#x2F;tmp&#x2F;sh var=variables x=$( cat &lt;&lt;EOT This string has &#x27;single&#x27; and &quot;double&quot; quotes and can interpolate &#x27;$var&#x27; EOT ) echo $x % bash &#x2F;tmp&#x2F;sh This string has &#x27;single&#x27; and &quot;double&quot; quotes and can interpolate &#x27;variables&#x27;</code></pre>
bhpnpmgover 4 years ago
I wrote bash function that leverages &#x27;set -x&#x27; to get me the quoting in &quot;$@&quot; into a single bash env var say $job or in to a temp file. I use it from time to time -- pretty sure it&#x27;s not perfect, but it works well enough to be useful. To use it it usually involves an &#x27;eval&#x27;.
评论 #24667961 未加载
评论 #24667947 未加载
gigatexalover 4 years ago
The only thing that trips me up more on becoming a true grey beard other than regex mastery is how and when to properly quote things in my command line incantations.
unityByFreedomover 4 years ago
This is hands down the coolest thing I&#x27;ve learned this year. Thank you
zeepzeepover 4 years ago
Oh I thought like, end a string without a quote...
tus88over 4 years ago
Try doing it with :#s&#x2F;&#x2F;&#x2F;
评论 #24660286 未加载
x87678rover 4 years ago
Just another reminder bash scripts are unnecessarily complicated, use python instead.