V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
noanti
V2EX  ›  问与答

shell 程序的提示符是怎么实现的?

  •  
  •   noanti · Jan 28, 2015 · 2974 views
    This topic created in 4113 days ago, the information mentioned may be changed or developed.
    root@ubuntu:
    这不是写到标准输出的,是怎么显示出来的呢?
    10 replies    2015-01-28 17:56:19 +08:00
    chunyang
        1
    chunyang  
       Jan 28, 2015
    通过设置环境变量 PS1, PS2, PS3, PS4,`man bash` 中也有解释:

    > PS1 The value of this parameter is expanded (see PROMPTING below) and used as the primary prompt string. The default value is ``\s-\v\$ ''.
    superbear
        2
    superbear  
       Jan 28, 2015
    可通过配置环境变量PS1来实现,
    具体可看这篇文章[Linux下PS1、PS2、PS3、PS4使用详解](os.51cto.com/art/201205/334954.htm)
    noanti
        3
    noanti  
    OP
       Jan 28, 2015
    @chunyang
    @superbear
    我并不是想修改提示符……我的意思是提示符是怎么显示在终端上的?比如bash的user@host,并不是bash程序把这个字符串写到了标准输出上。
    我要是想自己写一个shell程序,应该怎么实现提示符?
    lululau
        4
    lululau  
       Jan 28, 2015
    大概是下面这样?

    print(expand-ps1(value-of-ps1))

    while (cmd = stdin.read-line()) {
    do-expansions-and-substitutions
    fork and exec cmd
    print(expand-ps1(value-of-ps1))
    }
    chunyang
        5
    chunyang  
       Jan 28, 2015
    > root@ubuntu:
    > 这不是写到标准输出的,是怎么显示出来的呢?

    写到 STDOUT 也”能“显示出来了,至于标准 I/O 是如何工作的,可能涉及到文件系统的知识,我不太了解。
    sNullp
        6
    sNullp  
       Jan 28, 2015
    @noanti 明明就是写到stdout。。。
    zealic
        7
    zealic  
       Jan 28, 2015
    @sNullp Shell 程序一般都会申请一个 tty/pts,往这个设备里面写入。
    具体可以看 《Unix 高级编程》关于 tty 那部分。
    sNullp
        8
    sNullp  
       Jan 28, 2015 via iPhone
    @zealic 学习了,多谢!
    noanti
        9
    noanti  
    OP
       Jan 28, 2015
    @sNullp 不是的,你用管道把sh的标准输出写到文件,是没有提示符的。
    shoumu
        10
    shoumu  
       Jan 28, 2015
    我记得我们实现shell的时候就是直接写到stdout的,看了楼上的,学习了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   800 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 21:55 · PVG 05:55 · LAX 14:55 · JFK 17:55
    ♥ Do have faith in what you're doing.