How to expand PS1?

后端 未结 7 1173
孤城傲影
孤城傲影 2020-11-27 17:57

I have a shell script that runs the same command in several directories (fgit). For each directory, I would like it to show the current prompt + the command which will be ru

相关标签:
7条回答
  • 2020-11-27 18:44

    One great advantage of open source software is that the source is, well, open :-)

    Bash itself does not provide this functionality but there are various tricks you can use to provide a subset (such as substituting \u with $USER and so on). However, this requires a lot of duplication of functionality and ensuring that the code is kept in sync with whatever bash does in future.

    If you want to get all the power of prompt variables (and you don't mind getting your hands dirty with a bit of coding (and, if you do mind, why are you here?)), it's easy enough to add to the shell itself.

    If you download the code for bash (I'm looking at version 4.2), there's a y.tab.c file which contains the decode_prompt_string() function:

    char *decode_prompt_string (string) char *string; { ... }
    

    This is the function that evaluates the PSx variables for prompting. In order to allow this functionality to be provided to users of the shell itself (rather than just used by the shell), you can follow these steps to add an internal command evalps1.

    First, change support/mkversion.sh so that you won't confuse it with a "real" bash, and so that the FSF can deny all knowledge for warranty purposes :-) Simply change one line (I added the -pax bit):

    echo "#define DISTVERSION \"${float_dist}-pax\""
    

    Second, change builtins/Makefile.in to add a new source file. This entails a number of steps.

    (a) Add $(srcdir)/evalps1.def to the end of DEFSRC.

    (b) Add evalps1.o to the end of OFILES.

    (c) Add the required dependencies:

    evalps1.o: evalps1.def $(topdir)/bashtypes.h $(topdir)/config.h \
               $(topdir)/bashintl.h $(topdir)/shell.h common.h
    

    Third, add the builtins/evalps1.def file itself, this is the code that gets executed when you run the evalps1 command:

    This file is evalps1.def, from which is created evalps1.c.
    It implements the builtin "evalps1" in Bash.
    
    Copyright (C) 1987-2009 Free Software Foundation, Inc.
    
    This file is part of GNU Bash, the Bourne Again SHell.
    
    Bash is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    Bash is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
    
    $PRODUCES evalps1.c
    
    $BUILTIN evalps1
    $FUNCTION evalps1_builtin
    $SHORT_DOC evalps1
    Outputs the fully interpreted PS1 prompt.
    
    Outputs the PS1 prompt, fully evaluated, for whatever nefarious purposes
    you require.
    $END
    
    #include <config.h>
    #include "../bashtypes.h"
    #include <stdio.h>
    #include "../bashintl.h"
    #include "../shell.h"
    #include "common.h"
    
    int
    evalps1_builtin (list)
         WORD_LIST *list;
    {
      char *ps1 = get_string_value ("PS1");
      if (ps1 != 0)
      {
        ps1 = decode_prompt_string (ps1);
        if (ps1 != 0)
        {
          printf ("%s", ps1);
        }
      }
      return 0;
    }
    

    The bulk of that is the GPL licence (since I modified it from exit.def) with a very simple function at the end to get and decode PS1.

    Lastly, just build the thing in the top level directory:

    ./configure
    make
    

    The bash executable that appears can be renamed to paxsh, though I doubt it will ever become as prevalent as its ancestor :-)

    And running it, you can see it in action:

    pax> mv bash paxsh
    
    pax> ./paxsh --version
    GNU bash, version 4.2-pax.0(1)-release (i686-pc-linux-gnu)
    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.
    
    pax> ./paxsh
    
    pax> echo $BASH_VERSION
    4.2-pax.0(1)-release
    
    pax> echo "[$PS1]"
    [pax> ]
    
    pax> echo "[$(evalps1)]"
    [pax> ]
    
    pax> PS1="\h: "
    
    paxbox01: echo "[$PS1]"
    [\h: ]
    
    paxbox01: echo "[$(evalps1)]"
    [paxbox01: ]
    

    When you put one of the PSx variables into the prompt, echoing $PS1 simply gives you the variable, while the evalps1 command evaluates it and outputs the result.

    Now, granted, making code changes to bash to add an internal command may be considered by some to be overkill but, if you want an perfect evaluation of PS1, it's certainly an option.

    0 讨论(0)
提交回复
热议问题