免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1967 | 回复: 3
打印 上一主题 下一主题

关于shell提示符PS1自定义的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-05-21 22:24 |只看该作者 |倒序浏览
最近捣鼓了下颜色终端,将自己的提示符弄得焕然一新:
  1.             PS1="\[\e[01;31m\][\[\e[33m\]\u\[\e[32m\]@\[\e[37m\]\h:\[\e[04m\]\w\[\e[m\]\[\e[01;31m\]]\[\e[31m\] \\$\[\e[m\] "  <--- 配置提示符颜色方案
  2.                 
复制代码



# var="\033[01m[\033[33m\u\033[32m@\033[37m\h:\033[04m\w\033[01;37m\033[24;37m]\033[34m \\$\033[0m "
   PS1=$var        <--- 效果一样但对命令行有影响
                               但会导致命令行输入字符宽度为固定的83(含提示符所在个数),并且超过83个字符后不能自动换行,而是回到行首覆盖输入的不良后果



以上问题很奇怪,还请大侠们看看,这是为什么???:wink:

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
2 [报告]
发表于 2012-05-22 06:25 |只看该作者

  1. 3.4. Non-Printing Characters in Prompts

  2. Many of the changes that can be made to Bash prompts that are discussed in this HOWTO use non-printing characters. Changing the colour of the prompt text, changing an Xterm title bar, and moving the cursor position all require non-printing characters.

  3. If I want a very simple prompt consisting of a greater-than sign and a space:

  4. [giles@nikola giles]$ PS1='> '
  5. >

  6. This is just a two character prompt. If I modify it so that it's a bright yellow greater-than sign (colours are discussed in their own section):

  7. > PS1='\033[1;33m>\033[0m '
  8. >

  9. This works fine - until you type in a large command line. Because the prompt still only consists of two printing characters (a greater-than sign and a space) but the shell thinks that this prompt is eleven characters long (I think it counts '\033' , '[1' and '[0' as one character each). You can see this by typing a really long command line - you will find that the shell wraps the text before it gets to the edge of the terminal, and in most cases wraps it badly. This is because it's confused about the actual length of the prompt.

  10. So use this instead:

  11. > PS1='\[\033[1;33m\]>\[\033[0m\] '

  12. This is more complex, but it works. Command lines wrap properly. What's been done is to enclose the '\033[1;33m' that starts the yellow colour in '\[' and '\]' which tells the shell "everything between these escaped square brackets, including the brackets themselves, is a non-printing character." The same is done with the '\033[0m' that ends the colour.
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-05-22 13:06 |只看该作者
回复 2# waker

Thanks....:wink:

你的答案十分解惑,这里我也分享一下我查找到的相关网页资料:
http://archive09.linux.com/base/ldp/howto/HOWTO-INDEX/index.html       <--- 应该就是你上面信息的出处吧^_^

另外我在wiki百科中也找到了类似的信息:

https://wiki.archlinux.org/index.php/Color_Bash_Prompt    <--- Color Bash Prompt


   

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
4 [报告]
发表于 2012-05-22 15:55 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP