92

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?

3

5 Answers 5

164
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.

Sign up to request clarification or add additional context in comments.

8 Comments

An uglier form is: `'I'"'"'m a student'
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 What versions have problems with this? I don't see that error in bash 4.1.2.
@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.
And this kind of stuff is why I use Python as a replacement for Bash scripts now.
|
79

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!

4 Comments

I think our definitions of beauty differ somewhat.. ;)
Strictly speaking, it doesn't answer the question of how to escape a single quote inside a single-quoted string ;-)
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.
Why not just echo I"'"m a student?
38

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

echo 'I'\''m a student' 

3 Comments

This seems to be the best answer. I guess it's not highly voted cause it's been posted several years after the previous answers.
Much better than the answer above and far less confusing!
on scp gives error unexpected EOF while looking for matching `''
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' 

Comments

-5

I propose

echo "I'm a student"

like in other languages.

1 Comment

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 ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.