How to expand PS1?

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

    One more possibility: without editing bash source code, using script utility (part of bsdutils package on ubuntu):

    $ TEST_PS1="\e[31;1m\u@\h:\n\e[0;1m\$ \e[0m"
    $ RANDOM_STRING=some_random_string_here_that_is_not_part_of_PS1
    $ script /dev/null <<-EOF | awk 'NR==2' RS=$RANDOM_STRING
    PS1="$TEST_PS1"; HISTFILE=/dev/null
    echo -n $RANDOM_STRING
    echo -n $RANDOM_STRING
    exit
    EOF
    
    

    script command generates a file specified & the output is also shown on stdout. If filename is omitted, it generates a file called typescript.

    Since we are not interested in the log file in this case, filename is specified as /dev/null. Instead the stdout of the script command is passed to awk for further processing.

    1. The entire code can also be encapsulated into a function.
    2. Also, the output prompt can also be assigned to a variable.
    3. This approach also supports parsing of PROMPT_COMMAND...

提交回复
热议问题