89

I want to display a string in Bash like this:

I'm a student

Of course you can do it like this:

echo "I'm a student"

But how can I accomplish this while using single quotes around the string ?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
爱国者
  • 4,298
  • 9
  • 47
  • 66
  • 4
    Sorry, there is no way to place a single quote in between single quotes. See http://www.gnu.org/software/bash/manual/bashref.html#Special-Parameters on section `3.1.2.2 Single Quotes` – 爱国者 Nov 24 '11 at 08:41
  • And that in a script, how do you do? `su -c '/etc/init.d/myservice start' -l myuser` (run by root) – Sandburg Jan 31 '19 at 09:54
  • The canonical is probably *[How to escape single quotes within single quoted strings](https://stackoverflow.com/questions/1250079/)* (2009. 26 answers. 1380 votes). [An answer](https://stackoverflow.com/questions/1250079/how-to-escape-single-quotes-within-single-quoted-strings/16605140#16605140) covers the same as the top answer here. – Peter Mortensen Aug 16 '23 at 18:27

5 Answers5

161
echo 'I\'m a student'

does not work. But the following works:

echo $'I\'m a student'

From the man page of bash:

A single quote may not occur between single quotes, even when preceded by a backslash.
....
Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 5
    An uglier form is: `'I'"'"'m a student' – glenn jackman Nov 24 '11 at 19:36
  • 4
    `echo $'I\'m a student!'` => `!': event not found`, this is not a *real* single quoted string which, in bash, should protect from any interpretation. – regilero May 28 '14 at 15:26
  • 1
    @regilero What versions have problems with this? I don't see that error in bash 4.1.2. – sappjw Dec 01 '14 at 15:29
  • @sappjw OS X bash: `$ bash --version` `GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15) Copyright (C) 2007 Free Software Foundation, Inc.` – Vova Rozhkov Oct 31 '16 at 13:06
  • And this kind of stuff is why I use Python as a replacement for Bash scripts now. – sudo Jan 27 '17 at 18:58
  • re: `os/x` bash: this answer works for me in bash on same version `bash --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15) Copyright (C) 2007 Free Software Foundation, Inc.` – WestCoastProjects Apr 28 '17 at 19:48
  • @regilero according to the manual, it is `The expanded result is single-quoted, as if the dollar sign had not been present.` https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#ANSI_002dC-Quoting – CervEd Sep 13 '22 at 12:22
77

The "ugly" solution mentioned by Glenn Jackman should actually be listed as a top level answer. It works well and is actually beautiful in some situations.

'I'"'"'m a student'

This ends the single quoted string after I then immediately starts a double quoted string containing a single quote and then starts another single quoted string. Bash then concatenates all contiguous strings into one.

Beautiful!

Mark
  • 2,380
  • 11
  • 29
  • 49
Luke Gedeon
  • 892
  • 7
  • 14
  • 52
    I think our definitions of beauty differ somewhat.. ;) – Håvard Geithus Oct 05 '14 at 13:16
  • 3
    Strictly speaking, it doesn't answer the question of how to escape a single quote inside a single-quoted string ;-) – Roland Weber Mar 22 '19 at 12:30
  • the underlying most common problematic is wanting to go "level n" on refactoring code. this does answer that demand. while the `$` does not necessarily have "+n" capacity. I'm not sure I'll have to test more. but it look to me like I'll be able to execute code that was already in single quotes stored into separate variables thanks to this. – tatsu Mar 31 '19 at 12:43
  • Why not just `echo I"'"m a student`? – wk_j Nov 22 '22 at 23:37
35

The example below works because the escaped single quote \' is technically between two single-quoted arguments

echo 'I'\''m a student'
wscourge
  • 10,657
  • 14
  • 59
  • 80
Nick Jensen
  • 411
  • 4
  • 5
0

Another way to workaround is to use printf instead echo and escape the required single quote with \x27:

printf 'I\x27m a student!\n'
Haru
  • 224
  • 1
  • 5
-4

I propose

echo "I'm a student"

like in other languages.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Sab
  • 63
  • 2
  • 3
  • 4
    Doesn't the question exclude this? *Of course you can do it like this `echo "I'm a student"` But how to accomplish this while using single quote around the string ?* – Jeff Schaller Jan 23 '23 at 19:51