Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Post bash quotes

Name: Anonymous 2011-04-11 20:12

One recurring complaint is that nobody talks about bash quotes on /prog/. So everyone post some bash quotes. A small explanation wouldn't go amiss either.

I'll start us off with a /prog/ related one from the top 100:

<kow`> "There are 10 types of people in the world... those who understand binary and those who don't."
<SpaceRain> That's only 2 types of people, kow.

The joke here is that 10 in binary is actually 2 in base 10. So when kow` mentioned that there were 10 types of people in the world, the "10" was in binary since those who understand that would be able to understand the joke.
<SpaceRain> STUPID

Name: Anonymous 2011-04-11 20:14

Using single quotes in BASH ensures that the shell doesn’t expand the contents of the quoted string and this is useful most of the time. However if you want to use single quotes within a single quoted string things don’t work out as you might expect.

For example using the following simple PHP snippet:

<?php
echo '<p>this is a string</p>';
?>


If I want to use sed to match some text with a single quotes in it, I will run into trouble if I run:

sed -e s/'<p>/'<p class="test">/g

This will output:

/bin/bash: line 1: p: No such file or directory

This is because the unquoted string will be expanded and BASH will think that < is a redirection.

Alternatively if I run (on the same snippet):

sed -e 's/\'<p>/\'<p class="test">/g'

I will get:

/bin/bash: -c: line 1: unexpected EOF while looking for matching `''
/bin/bash: -c: line 2: syntax error: unexpected end of file


This doesn’t work because the escaped single-quotes (\') are not expanded and are therefore treated literally.

To single quotes work you need to break out of the single quoted string then escape your single quote. Like so:

sed -e 's/'\''<p>/'\''<p class="test">/g'

Because \' is not inside of single quotes the single-quote is properly escaped and the output is as we’d expect:

<?php
echo '<p class="test">this is a string</p>';
?>


In conclusion, the title of this post is a bit of a misnomer. You actually can’t put single quotes inside of a single-quoted string. However breaking out allows us to get to where we want to be.

Name: Anonymous 2011-04-11 21:53

>>2
Why do you have to ruin threads?

Name: Anonymous 2011-04-11 22:21

>>3
Because he is a huge faggot please rape his face.

Name: Anonymous 2011-04-11 22:29

In Bash, whether to use single or double quotes depends on exactly what you want to do, and the differences can trip you up if you're not concentrating. Here's a quick rundown of what each does and when to use them.

Double quotes are the less restrictive option. Within a double-quoted string, the only special characters that are reinterpreted are $, ` (backquote), and (the escape character). Double quotes are useful when using variables (in case the variable uses other special characters), or to avoid whitespace being used to make one argument into two. For example:

message="My commit message"
git commit -m $message


Here, $message will be interpreted as three separate words, so the only part that will be used as the commit message is "My". "commit" and "message" will be treated as files to look for, and the commit will probably fail. Use double quotes to fix this:

message="My commit message"
git commit -m "$message"

Name: Anonymous 2011-04-11 22:48

>>5 see >>4

Name: Anonymous 2011-04-11 23:00

cit gommit

Name: Anonymous 2011-04-11 23:55

Wallace and Gromit.

Name: Anonymous 2011-04-12 7:40

Sometimes I use backtick quotes in bash instead of $(...).

Name: Anonymous 2011-04-12 13:12

>>6 see >>3

Don't change these.
Name: Email:
Entire Thread Thread List