Skip to content
wileyhy edited this page Oct 6, 2024 · 8 revisions

\t is just literal t here. For tab, use "$(printf '\t')" instead.

Problematic code:

# Want tab
$ var=foo\tbar
$ printf '<%s>\n' "$var"
<footbar>

or

# Want linefeed
$ var=foo\nbar
$ printf '<%s>\n' "$var"
<foonbar>

Correct code:

$ var="foo$(printf '\t')bar"  # As suggested in warning
$ printf '<%s>\n' "$var"
<foo	bar>
$ var="$(printf 'foo\tbar')"  # Equivalent alternative
$ printf '<%s>\n' "$var"
<foo	bar>

or

# Literal, quoted linefeed
$ line="foo
> bar"
$ printf '<%s>\n' "$line"
<foo
bar>

or

# Linefeed using ANSI-C quoting
$ line=$'foo\nbar'
$ printf '<%s>\n' "$line"
<foo
bar>

Rationale:

ShellCheck has found a \t, \n or \r in a context where they just become regular letters t, n or r. Most likely, it was intended as a tab, linefeed or carriage return.

To generate such characters (plus other less common ones including \a, \f and octal escapes) , use printf as in the example. The exception is for linefeeds that would be stripped by command substitution; in these cases, use a literal quoted linefeed instead.

Other characters like \z generate a SC1001 info message, as the intent is less certain.

Exceptions:

None.

Related resources:

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally