sthbuilder
sthbuilder

Reputation: 573

escape character usage in bash, what do we actually escape?

I am reading the bash manual, found the escape character definition pretty surprising, instead of modifying the meaning of every character follows it (as in Java / C):

It preserves the literal value of the next character that follows

Does it mean in bash, we only use it to escape special meaning character like ', ", \, $

And other cases, like \t\e\s\t actually is exactly as test ? I verified that

echo test

echo \t\e\s\t outputs same result.

Upvotes: 0

Views: 307

Answers (1)

Amadan
Amadan

Reputation: 198436

Does it mean in bash, we only use it to escape special meaning character like ', ", \, $

Yes. Also, e.g. newline:

echo foo
bar
# foo
# -bash: bar: command not found

echo foo \
bar
# foo
# bar

And other cases, like "\t\e\s\t" actually is exactly as "test"

If unquoted, yes. Quoted, the backslash is preserved. Some UNIX utilities do use backslash for "special meanings", but it is the utility, not bash, that gives those sequences meanings. Examples are printf, and GNU echo when given -e option:

/bin/echo \t\e\s\t
# test

/bin/echo "\t\e\s\t"
# \t\e\s\t

/bin/echo -e "\t\e\s\t"      # GNU version (will not do the same thing on Mac)
#         s       
# (tab)(escape)s(tab)

printf "\t\e\s\t"
#         s       
# (tab)(escape)s(tab)

As @rici reminds me, bash can interpret C-style escape sequences itself, if you use the special quotes of the form $'...':

/bin/echo $'\t\e\s\t'
#         s       

Here it really is bash that does it, not echo.

Upvotes: 1

Related Questions