ls --color的使用

1,问题的发现:

今天写了一个句非常简单的shell,结果发现和预期的结果不同:

$ ls -1 | tail -1
$

本意是获取当前目录下默认排序的最后一个文件,可是却没有能够拿到任何信息。

可是我写在一个脚本里的这句话却得到了正确的结果:

$ cat test.sh
#!/bin/bash
ls -1 | tail -1
$ ./test.sh
test.sh

最后发现是因为我的当前bash下,ls是被我使用了别名:

$ type ls
ls is aliased to `ls -F --color'

关键就在

$ /bin/ls -1 | tail -1
test.sh

于这个--color  去掉后就好了. 应该就是--color的颜色控制字符导致的,但是为什么最后一行为空就没有仔细研究了。

2, 问题的解决

如何解决这个问题呢,看了ls的help文件后,有这样一句:

With --color=auto, color codes are output
       only if standard output is connected to a terminal (tty)

所以使用--color=auto就好了。

$ ls --color | tail -1
$ ls --color=auto | tail -1
test.sh*

3, 问题的发展

回家后发现和ls的版本也有关系

$ /bin/ls --color | tail -1
$ /usr/local/bin/ls --color | tail -1
Workspace

$ /bin/ls --v
ls (GNU coreutils) 5.97
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard Stallman and David MacKenzie.
$ /usr/local/bin/ls --v
ls (GNU coreutils) 8.12
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David MacKenzie.

但是无论如何,--color=auto是最保险的。

自己常用的还有grep --color 也改用了 --color=auto  ^_^ 只是最简单的记录

转载于:https://my.oschina.net/maxio/blog/519320

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值