-1

We can use echo $PS1 to view

test@testauto:~$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
test@testauto:~$

But what I want is "test@testauto:~$" not "[\e]0;\u@\h: \w\a]${debian_chroot:+($debian_chroot)}\u@\h:\w$"

How can I get it?

  • Just an aside, I always found the user prompt of `"\[\e[38;5;244m\]\D{%R}\[\e[38;5;32m\] \h:\w> \[\e[0m\]"` and a root prompt of `"\[\e[1;34m\][\[\e[38;5;203m\]\A \[\e[1;34m\]\h\[\e[38;5;197m\]:\w\[\e[1;34m\]] # \[\e[0m\]"` helpful. (256 color xterm) In addition to providing the time, they also provide the current path information in a form that can be used with `rsync`, `scp`, etc... E.g. user prompt of `00:48 wizard:~/tmpd> ` and root prompt (same location) `[00:48 wizard:/home/david/tmpd] # ` – David C. Rankin Jun 08 '21 at 05:50
  • @GavinHunte21 : You tagged your question as _shell_, i.e. POSIX shell. See [here](https://pubs.opengroup.org/onlinepubs/9699919799/) for an explanation what you can put into `PS1`. It is not clear to me, how your question fits into the idea of POSIX shell. – user1934428 Jun 08 '21 at 08:56

1 Answers1

1

For Bash

echo "${PS1@P}"

For ZSH

echo "${(%%)PS1}"

You can find more about this in this thread: How to print current bash prompt?

Kerrim
  • 485
  • 4
  • 9
  • I tested it on Ubuntu and the result was as expected. I tested it on an embedded device which using busybox and it failed. `test_aut:~ # echo "${PS1@P}" /bin/sh: syntax error: bad substitution test_aut:~ # ls -al /bin/sh lrwxrwxrwx 1 root 0 7 May 25 05:14 /bin/sh -> busybox` Any comments? Thanks in advance. – Gavin Hunter21 Jun 08 '21 at 06:07
  • @GavinHunter21 this is because busybox is not using bash or zsh. According tho this thread https://serverfault.com/questions/241959/whats-the-busybox-default-shell its some derivative of ash. I'm not familiar with busybox but after taking a look at the code I'm not sure if it supports that. – Kerrim Jun 09 '21 at 05:22