1

I'm using https://github.com/magicmonty/bash-git-prompt to customize my bash prompt. so, my prompt usually looks something like this

✔ ~/dotfiles [master|⚑ 1] 15:00 $

is there any command with which I can capture that prompt text? I want the [master|⚑ 1] text without having to copy the bash-git-prompt code into a new shell script.

2

if you're using bash 4.x, you can capture it into a variable

prompt="${PS1@P}"

or just output it for capture into a pipeline

echo "${PS1@P}"

You can also use script to capture a terminal session - including the prompt as displayed - to a file which you can then trim and use for whatever nefarious purposes:

$ script foo
user@host:~
$ exit
$ cat foo
Script started on Thu Jul  6 12:59:45 2017
user@host:~
$ exit
exit
Script done on Thu Jul  6 12:59:48 2017

Not shown here are all the ANSI color codes which spiff up the prompt.

3
  • i'm on bash 3.2 – kenwarner Jul 6 '17 at 21:06
  • @DopeGhoti This doesn't work. I am in bash4.3 – SibiCoder Feb 20 '20 at 8:35
  • Define 'doesn't work'. script is an external tool, so it doesn't care what version of bash (or indeed what shell) you're using. – DopeGhoti Feb 20 '20 at 14:17
-1

Found a good solution here https://stackoverflow.com/a/44090232/55948

eval 'echo -en "'$PS1'"'

2
  • This will not show the expanded prompt. – DopeGhoti Jul 6 '17 at 22:31
  • you usually go around downvoting 2 year old answers from noobs?? just leave the comment maybe – kenwarner Aug 1 '19 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.