Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I insert a string containing escaped characters (\n, \t, etc) as separator for the seq command?

The standard format includes a newline character:

    $ seq 3

But if I try to add something plus a newline, the backslash is escaped and a literal "\n" is used instead:

    $ seq -s "$\n" 3

The same happens using simple quotes, no quotes, or other escaped characters:

    $ seq -s "\t" 3

    $ seq -s \t 3

This is not the standard behavior for commands such as echo, so I'm a bit confused here...

Edit: Ideally, I'd like a somewhat portable solution (that works in tsch as well as bash, for instance), and without resorting to Perl or other languages.

share|improve this question
up vote 2 down vote accepted

In bash, instead of trying to enter the newline character as an escape sequence, simply type it as a newline in the middle of the quoted string by pressing Enter. For example,

bash-4.1$ seq -s "$
> " 3
share|improve this answer
Thanks, I'll keep it in mind, but if someone can supply a more "portable" solution I'd prefer it. – anol Dec 12 '12 at 16:51
This is the portable solution. The problem is that bash doesn't process escape sequences in arguments except for the internal echo command. For other commands, the argument is passed straight through to the command, which may or may not support escape sequences. seq apparently does not. – Nicole Hamilton Dec 12 '12 at 16:53
Sorry for misunderstanding. But for me, it works in bash but not tcsh, so I thought it was the other way around. But I do believe @terdon's solution can be more useful since it allows for instance to use \t in both bash and tsch, while I cannot type a literal Tab in any of those using gnome-terminal. – anol Dec 12 '12 at 17:00
You'll have the same problem in tcsh. It also only processes escapes for echo. You still need the quotes, but to enter a newline is a little different: You have to type a backslash just before the newline. – Nicole Hamilton Dec 12 '12 at 17:01
Yes, tab is more difficult in both those shells. But in tcsh, you'll have trouble making @terdon's solution work because tcsh command substitution syntax (using backticks rather than $(...)) doesn't lend itself to quotes inside quotes, meaning it'll be hard to avoid turning the tab into a space. – Nicole Hamilton Dec 12 '12 at 17:10

To get tabs and other strange characters you could also try this:

$ seq -s "$(echo -e '\t')" 3
1   2   3
$ seq -s "$(echo -e '\n$')" 3
share|improve this answer
Nice! I can adapt it to tcsh as follows: seq -s "`echo '\t'`" 3. The only problem is the newline doesn't work that way: running seq -s "`printf '\n'`" 3 gives seq: missing operand since the newline is processed before the end of the command, I think. – anol Dec 12 '12 at 16:55
It's not printf, since the same happens with echo as well in tcsh, when trying to insert a newline. Does it work for you? I'm using gnome-terminal, but since it works in bash I'm not sure the problem is the terminal. I'm probably accepting your answer, just waiting a little bit in case someone comes with a solution that works in all terminals, for all characters, without Perl or other languages (though I'm not sure such an "ideal" solution exists). – anol Dec 12 '12 at 17:02
Too many good answers, it's hard to pick the best one... but I did upvote all those which taught me something, thanks! – anol Dec 12 '12 at 17:14

At least in bash, you can use strings that are interpreted like in ANSI C by using the format $'string' (these must be single quotes).

seq -s $'$\n' 3

tcsh complains about an illegal variable name, unfortunately.

share|improve this answer

I don't know -- but as an alternative

$ perl -e 'printf "%9d\n",$_ for 6..15'




$ perl -le 'print "\$$_" for 1..5'


share|improve this answer

seq -s "$(echo -e '\n')" 3 doesn't work. A possible solution: seq -s "$(echo -e '\n\r')" 3 | tr -d '\r'

share|improve this answer
What platform and shell are you using? – suspectus Aug 28 '15 at 8:27
seq -s "$(echo -e '\n')" 3 prints "123", without \n. Reason: The output of $(echo -e '\n') is "\n\n"; the newline character is in the IFS variable, so the shell executes seq -s "" 3. Solution: seq -s $'\n' 3 – Antonio Lebrón Dec 1 '15 at 11:17
My previous answer is incorrect: the output of the command substitution is literal because the double quotes. True reason: All trailing newlines are removed in command substitution according bash documentation. – Antonio Lebrón Dec 4 '15 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .