3

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
    1
    2
    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
    1\n2\n3

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

    $ seq -s "\t" 3
    1\t2\t3

    $ seq -s \t 3
    1t2t3

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.

5 Answers 5

2

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
1$
2$
3
bash-4.1$
8
  • Thanks, I'll keep it in mind, but if someone can supply a more "portable" solution I'd prefer it.
    – anol
    Dec 12, 2012 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. Dec 12, 2012 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, 2012 at 17:00
  • 1
    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. Dec 12, 2012 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. Dec 12, 2012 at 17:10
2

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
1
$2
$3
3
  • 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, 2012 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, 2012 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, 2012 at 17:14
1

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
1$
2$
3

tcsh complains about an illegal variable name, unfortunately.

0

I don't know -- but as an alternative

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

produces

        6
        7
        8
        9
       10
       11
       12
       13
       14
       15

and

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

produces

$1
$2
$3
$4
$5
0

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

3
  • What platform and shell are you using?
    – suspectus
    Aug 28, 2015 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 Dec 1, 2015 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. Dec 4, 2015 at 14:07

You must log in to answer this question.

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