Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

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
1$
2$
3
bash-4.1$
share|improve this answer
    
Thanks, I'll keep it in mind, but if someone can supply a more "portable" solution I'd prefer it. –  dhekir 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. –  dhekir Dec 12 '12 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. –  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
1
$2
$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. –  dhekir 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). –  dhekir 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! –  dhekir 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
1$
2$
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'

produces

        6
        7
        8
        9
       10
       11
       12
       13
       14
       15

and

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

produces

$1
$2
$3
$4
$5
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.