How to expand PS1?

后端 未结 7 1195
孤城傲影
孤城傲影 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:40

    I like the idea of fixing Bash to make it better, and I appreciate paxdiablo's verbose answer on how to patch Bash. I'll have a go sometime.

    However, without patching Bash source-code, I have a one-liner hack that is both portable and doesn't duplicate functionality, because the workaround uses only Bash and its builtins.

    x="$(PS1=\"$PS1\" echo -n | bash --norc -i 2>&1)"; echo "'${x%exit}'"
    

    Note that there's something strange going on with tty's and stdio seeing as this also works:

    x="$(PS1=\"$PS1\" echo -n | bash --norc -i 2>&1 > /dev/null)"; echo "'${x%exit}'"
    

    So although I don't understand what's going on with the stdio here, my hack is working for me on Bash 4.2, NixOS GNU/Linux. Patching the Bash source-code is definitely a more elegant solution, and it should be pretty easy and safe to do now that I'm using Nix.

提交回复
热议问题