Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Why the following command prints Smith but not \Smith?

echo \Smith
share|improve this question

2 Answers 2

The backslash is an escape character that:

shall preserve the literal value of the following character, with the exception of a <newline>. ... The <backslash> ... shall be removed

So \S means the same thing as S, because S is not a newline character and also not a shell special character that could be escaped ($, ", ', {, [, `, \, |, &, ;, <, >, (, ), ?, *, [, #, ~, =, %, , or tab). To include a literal backslash, escape it in turn:

echo \\Smith

note: this is the specified behavior for your example unquoted case, but it can change if it is otherwise quoted or read in from a file

share|improve this answer

\S escapes the S, which is not a special character, thus gives S. You need to double the backslash to print it: echo \\Smith

EDIT: But more generally, it's better to use printf. See the difference between echo x\\by, which outputs "y" (the "x" gets overwritten by the backspace \b) with some versions of echo (dash, zsh), and printf "%s\n" x\\by, which outputs "x\by".

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.