其他方式(外部)
echo $out| head -c 20
echo $out | awk '{print substr($0,1,20) }'
echo $out | ruby -e 'print $_[0,19]'
echo $out | sed -r 's/(^.{20})(.*)/\1/'
Run Code Online (Sandbox Code Playgroud)
您可以使用命令替换来包装输出预重定向,然后使用偏移量参数扩展来限制字符数,如下所示:
#!/bin/bash
limit=20
out=$(echo "this line has more than twenty characters in it")
echo ${out::limit} > /path/to/file
Run Code Online (Sandbox Code Playgroud)
$ limit=20
$ out=$(echo "this line has more than twenty characters in it").
$ echo ${out::limit}
this line has more t
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3089 次 |
| 最近记录: |