Passing literal tab character from zsh to other program

I have a Zsh script which invokes another program. One of the paramters to be passed, should be a literal tab, i.e what in common programming languages is often written as "\t".

If it were bash, I think I could use the special form

$"\t"

but this doesn't seem to work (the called program sees a two-character string, backslash and t). I also tried putting octal code using $`010`, but no effect either (this is simply received as 010).

How can this be done in Zsh?

As far as I remember, $"..." notation is for language translation purposes.

Simply using "\t" woks for me.

imac5k% cat s1
#!/usr/bin/env zsh
./s2 "	" # literal tab
./s2 "\t"
imac5k% cat s2
#!/usr/bin/env zsh
echo "\"$1\""
imac5k% ./s1
"	"
"	"

In zsh try

$'\t'

instead of double quotes (which also works in bash).

--
@Scott, that is because of the echo statement. With printf it will print "\t"

2 Likes

"\t" works also with printf, but as $'\t' works, that's probably the way to go!

Hi Scott, yes what I meant is when you use printf in the safe/usual way (without data in the format argument):

printf '"%s"\n' "$1"

While $'\t' is easy to read and works in many current shells, it isn't in the standards yet and is not implemented by many shells in common use. Standard ways that should be supported by any shell based on Bourne shell syntax include:

utility_name "	" # where there is a literal tab character between the double-quote characters
utility_name '	' # where there is a literal tab character between the single-quote characters
utility_name "$(printf '\t')" # for modern shells
utility_name "$(printf '\011')" # for modern shells
utility_name "`printf '\t'`" # for 1980's vintage Bourne shells

and, of course, you can define a variable using any of those forms:

tab="	"
tab='	'
tab="$(printf '\t')"
tab="$(printf '\011')"
tab="`printf '\t'`"

and then use:

utility_name "$tab"