zsh-users
 help / color / mirror / code / Atom feed
* Using variables in command substitution
@ 2010-06-14  6:18 Dan Luther
  2010-06-14  9:02 ` Peter Stephenson
  2010-06-14 13:36 ` Benjamin R. Haskell
  0 siblings, 2 replies; 5+ messages in thread
From: Dan Luther @ 2010-06-14  6:18 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 779 bytes --]

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?

 

Thank you for your time.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Using variables in command substitution
  2010-06-14  6:18 Using variables in command substitution Dan Luther
@ 2010-06-14  9:02 ` Peter Stephenson
  2010-06-14 14:58   ` Dan Luther
  2010-06-14 13:36 ` Benjamin R. Haskell
  1 sibling, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2010-06-14  9:02 UTC (permalink / raw)
  To: Dan Luther; +Cc: zsh-users

On Mon, 14 Jun 2010 01:18:13 -0500
"Dan Luther" <dan@theluthers.net> wrote:
> 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:

You're hitting the fact that zsh doesn't split scalar variables
automatically.  See http://zsh.sourceforge.net/FAQ/zshfaq03.html#l18 .

A quick fix for the commands is to use arrays:

ME=(/usr/xpg4/bin/id -un)

etc.

-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Using variables in command substitution
  2010-06-14  6:18 Using variables in command substitution Dan Luther
  2010-06-14  9:02 ` Peter Stephenson
@ 2010-06-14 13:36 ` Benjamin R. Haskell
  2010-06-14 16:57   ` Dan Luther
  1 sibling, 1 reply; 5+ messages in thread
From: Benjamin R. Haskell @ 2010-06-14 13:36 UTC (permalink / raw)
  To: Dan Luther; +Cc: zsh-users

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Using variables in command substitution
  2010-06-14  9:02 ` Peter Stephenson
@ 2010-06-14 14:58   ` Dan Luther
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Luther @ 2010-06-14 14:58 UTC (permalink / raw)
  To: 'Peter Stephenson'; +Cc: zsh-users

Thanks -- this fixed my problem. You have to escape the "|" character, e.g.:

HNAME=(uname -n \| cut -d. -f1)
. . . 

Thank you again for prompt reply! 

-----Original Message-----
From: Peter Stephenson [mailto:Peter.Stephenson@csr.com] 
Sent: Monday, June 14, 2010 4:03 AM
To: Dan Luther
Cc: zsh-users@zsh.org
Subject: Re: Using variables in command substitution

On Mon, 14 Jun 2010 01:18:13 -0500
"Dan Luther" <dan@theluthers.net> wrote:
> 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:

You're hitting the fact that zsh doesn't split scalar variables
automatically.  See http://zsh.sourceforge.net/FAQ/zshfaq03.html#l18 .

A quick fix for the commands is to use arrays:

ME=(/usr/xpg4/bin/id -un)

etc.

-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ,
UK


Member of the CSR plc group of companies. CSR plc registered in England and
Wales, registered number 4187346, registered office Churchill House,
Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
No virus found in this incoming message.
Checked by AVG - www.avg.com 
Version: 9.0.829 / Virus Database: 271.1.1/2934 - Release Date: 06/13/10
14:45:00


^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Using variables in command substitution
  2010-06-14 13:36 ` Benjamin R. Haskell
@ 2010-06-14 16:57   ` Dan Luther
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Luther @ 2010-06-14 16:57 UTC (permalink / raw)
  To: 'Benjamin R. Haskell'; +Cc: zsh-users

Thank you -- that's VERY good information!

-----Original Message-----
From: Benjamin R. Haskell [mailto:zsh@benizi.com] 
Sent: Monday, June 14, 2010 8:37 AM
To: Dan Luther
Cc: zsh-users@zsh.org
Subject: 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 
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
No virus found in this incoming message.
Checked by AVG - www.avg.com 
Version: 9.0.829 / Virus Database: 271.1.1/2934 - Release Date: 06/14/10
01:35:00


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-06-15  2:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-14  6:18 Using variables in command substitution Dan Luther
2010-06-14  9:02 ` Peter Stephenson
2010-06-14 14:58   ` Dan Luther
2010-06-14 13:36 ` Benjamin R. Haskell
2010-06-14 16:57   ` Dan Luther

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