人们可以使用tab-width.sh这样的脚本:
#!/usr/bin/env bash
echo -ne 'a\tb' &&
echo -ne "\033[6n" # ask the terminal for the position
read -rs -d\[ _ # discard the first part of the response
read -rs -dR foo # store the position in bash variable 'foo'
foo=$(cut -d";" -f2 <<< "$foo") # discard row number
printf "\r\e[0K%d\n" $((foo - 2)) # subtract 2 for 'a' and 'b'
Run Code Online (Sandbox Code Playgroud)
改编自StackOverflow 上的这个答案。例子:
$ tabs 10
$ ./tab-width.sh
10
$ tabs 20
$ ./tab-width.sh
20
$ tabs 3
$ ./tab-width.sh
3
Run Code Online (Sandbox Code Playgroud)
请谨慎使用,该脚本尚未经过彻底测试。尽管 1 和 2 返回相同的值,但它在大多数情况下似乎运行良好:
$ tabs 1
$ ./tab-width.sh
2
$ tabs 2
$ ./tab-width.sh
2
Run Code Online (Sandbox Code Playgroud)
但应该注意到,echo -e 'a\tb'在这两种情况下,返回的输出也是相同的。
| 归档时间: |
|
| 查看次数: |
1758 次 |
| 最近记录: |