Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to print interpreted PS1 variable placeholders to console? Something like

$ echo `%u`

for printing a username or

$ echo `%h`

for printing a host. I do know about environment variables I just want to know if there is a way to get same information through PS1 variable.

share|improve this question
You should say which shell you're using. Especially if it's an uncommon one like tcsh! – Gilles Jul 27 '11 at 20:16
up vote 4 down vote accepted

In zsh, use the % parameter expansion flag.

tmp='%n@%m'; echo ${(%)tmp}

In bash, there doesn't seem to be an easy way, but there's a sneaky way:

tmp=$(set +x; (PS4='+.\u@\h'; set -x; :) 2>&1); tmp=${tmp#*.}; echo ${tmp%:}

In tcsh, I don't think there's a way. (But who uses (t)csh these days anyway?)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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