Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
2  
You should say which shell you're using. Especially if it's an uncommon one like tcsh! –  Gilles Jul 27 '11 at 20:16
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.