zsh-users
 help / color / mirror / code / Atom feed
From: "Benjamin R. Haskell" <zsh@benizi.com>
To: Dan Luther <dan@theluthers.net>
Cc: zsh-users@zsh.org
Subject: Re: Using variables in command substitution
Date: Mon, 14 Jun 2010 09:36:43 -0400 (EDT)	[thread overview]
Message-ID: <alpine.LNX.2.01.1006140924520.5029@hp.internal> (raw)
In-Reply-To: <001201cb0b89$63e2e3b0$2ba8ab10$@net>

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 
thing)

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

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

-- 
Best,
Ben


  parent reply	other threads:[~2010-06-15  2:13 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-14  6:18 Dan Luther
2010-06-14  9:02 ` Peter Stephenson
2010-06-14 14:58   ` Dan Luther
2010-06-14 13:36 ` Benjamin R. Haskell [this message]
2010-06-14 16:57   ` Dan Luther

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LNX.2.01.1006140924520.5029@hp.internal \
    --to=zsh@benizi.com \
    --cc=dan@theluthers.net \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).