Passing a tab character as a command line argument in Java

This question already has an answer here:

  • How to unescape a Java string literal in Java? 10 answers

  • The tab that you defined in code is a single-character string (since "t" is an escape sequence). From the command line you get a two-characters string, as bash doesn't handle "t" as the tab character.

    To pass that from command line, you can use echo -e which supports escape sequences like t (or n and a few others):

    $ java Main "$(echo -en 't')"
    

    If you pass 't' or "t" on your command line, it causes a literal backslash followed by a t to be passed to your command:

    $ echo -n 't' | xxd
    00000000: 5c74                                     t
    
    $ echo -n "t" | xxd
    00000000: 5c74                                     t
    

    You can type a tab on your command line using Ctrl-v then Tab:

    $ echo -n '     ' | xxd
    00000000: 09                                       .
    

    (That wide space is a tab character, not lots of spaces).


    To pass a tab as an argument from bash, you can do any of the following:

    mycommand $'argtcontainingttabs'
    Which works because $'' in bash interprets escape sequences.

    mycommand 'arg ctrl-vtab containing ctrl-vtab tabs'
    Which works because ctrl-v causes the next character typed to be inserted literally rather than being interpreted.

    mycommand "$(printf 'argtcontainingttabs')"
    Which works, in bash and other shells, because printf interprets backslash-escaped characters in its first argument.

    链接地址: http://www.djcxy.com/p/93896.html

    上一篇: GetAsync for fbcdn返回403禁止?

    下一篇: 在Java中传递制表符作为命令行参数