bash 如何在bash中设置4个空格选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10782699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set 4 space tab in bash
提问by Zheng Kai
It look like set tabstop=4
in VIM, but I don't know how to set it in bash
它看起来像set tabstop=4
在 VIM 中,但我不知道如何在bash 中设置它
for example:
例如:
echo -e "1234567890\t321\n1\t2\n123\t1"
current output:
电流输出:
1234567890 321
1 2
123 1
I want output like this:
我想要这样的输出:
1234567890 321
1 2
123 1
It can be shown in anywhere, just like cat somefile
or php -r 'echo "\t123";'
它可以显示在任何地方,就像cat somefile
或php -r 'echo "\t123";'
How can I set tab width in bash?
如何在 bash 中设置标签宽度?
回答by Mat
That's not a property of your shell (or php or cat). It's your terminal that manages the output.
这不是您的 shell(或 php 或 cat)的属性。管理输出的是您的终端。
Use the tabs
command to change the behavior:
使用tabs
命令更改行为:
$ tabs 4
$ echo -e "a\tb"
a b
$ tabs 12
$ echo -e "a\tb"
a b
(tabs
is specified in POSIX, and output above is "faked": it's still a tab character between the two letters.)
(tabs
在 POSIX 中指定,并且上面的输出是“伪造的”:它仍然是两个字母之间的制表符。)
回答by Paused until further notice.
You can set either regular or irregular intervals using the tabs
utility. It will work whether you're doing your own output, using cat
to output a file that already includes tabs or using the output of a program you don't control.
您可以使用该tabs
实用程序设置规则或不规则间隔。无论您是在做自己的输出,使用cat
输出已经包含选项卡的文件还是使用您无法控制的程序的输出,它都可以工作。
However, if you're controlling your output it's preferable to use printf
instead of echo
and format strings instead of tabs.
但是,如果您要控制输出,最好使用printf
代替echo
和格式化字符串而不是制表符。
$ printf '%-12s%8.4f %-8s%6.2f\n' 'Some text' 23.456 'abc def' 11.22
Some text 23.4560 abc def 11.22
$ format='%*s%*.*f %*s%*.*f\n'
$ printf "$format" -12 'Some text' 8 4 23.456 -8 'abc def' 6 2 11.22
Some text 23.4560 abc def 11.22
Unless you want someone else to be able to control the output of your program using the tabs
utility.
除非您希望其他人能够使用该tabs
实用程序控制程序的输出。
回答by carpii
You can use setterm to set this
您可以使用 setterm 来设置它
setterm -regtabs 4
I put it in my .bash_profile but its not bash related specifically
我把它放在我的 .bash_profile 中,但它不是专门与 bash 相关的