Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Using variables in command substitution

On Mon, 14 Jun 2010, Dan Luther wrote:

> Hello, 
> I'm hoping someone can explain this. 
> I'm trying to unify my .zshrc across a couple of platforms, and I ran across
> an interesting ZSH behavior. Essentially, I want to assign a variable to the
> name of a specific command for later substitution:
>   if [$(uname)="SunOS"]; then
>     ME="/usr/xpg4/bin/id -un"
>   elif [$(uname)="Linux"]; then
>     ME="id -un"
>   else
>     ME="who am i | cut -d ' ' -f1"
>   fi
>   HNAME=$(uname -n | cut -d. -f1)
> . . .
> cd() { chdir "$@"; prompt="[$($ME)@$HNAME] $(pwd)> "; }
>  . . . 
> When I try to use the "cd" redefinition, I get: 
> .zshrc: no such file or directory /usr/xpg4/bin/id -un
> Is it possible to use variables in command expansion at all, or am I 
> just doing it wrong?

Disregarding the problem as stated, a much simpler version of getting 
your prompt the way it seems you want it:

setopt prompt_subst
prompt='[%n@%m] %~> '

Vastly simpler than trying to find the information yourself across 
multiple systems.  Plus two improvements IMO:

1. shortened hostname (Use '%M' instead of '%m' if you want the full 

2. shortened pathnames ('%~' will try to compress the directory using 
named directories; use '%/' or '%d' [equivalent] if you want the full 

Look for 'SIMPLE PROMPT ESCAPES' in man zshall (probably in some 
subsection, but I never know which is which -- wasn't 'zshparam' or 


Messages sorted by: Reverse Date, Date, Thread, Author