From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: ${var:1:1:=y}
Date: Tue, 03 Feb 2015 19:25:07 -0800 [thread overview]
Message-ID: <150203192508.ZM2159@torch.brasslantern.com> (raw)
In-Reply-To: <54D155C8.4080600@eastlink.ca>
In-Reply-To: <412544FB-49A2-43AA-BC76-DC1AF1AA71BE@larryv.me>
In-Reply-To: <54D16A4C.9010609@eastlink.ca>
In-Reply-To: <B2986EC3-5BF2-4DBC-ABB9-7B6935FE2F91@larryv.me>
On Feb 3, 3:12pm, Ray Andrews wrote:
}
} I'm wanting the naive expansion of that syntax to do this:
}
} variable=${var:1:1:=y}
}
} ... but it doesn't work. Can something like that be done?
Jumping several messages ahead:
} assign to 'variable' either the value of the second character of 'var'
} (just one character), and if that does not exist, assign 'y'
You've forgotten that ${var=val} has the side-effect of assigning to
"var" if it is not set. I think what you mean is what Lawrence already
demonstrated (again several messages ahead):
variable=${${var:1:1}:-y}
However, you can do this with subscript syntax:
variable=${var[2]:=y}
which either assigns to $variable the second character of $var, or
assigns "y" to both $variable and to the second character of $var.
E.g.:
torch% var=a
torch% variable=${var[2]:=y}
torch% typeset -m var\*
variable=y
var=ay
torch%
You can also do ${var[2]::=z} to forcibly assign "z" to the second
character of $var.
On Feb 3, 7:18pm, Lawrence Velazquez wrote:
} Subject: Re: ${var:1:1:=y}
}
} Test cases should focus on the questionable behavior by discarding
} irrelevant details. Your case boils down to this:
}
} % echo -
}
} % /bin/echo -
} -
} %
Yes, the manual says (in the introduction to section 17 Shell Builtin
Commands and at the top of "man zshbuiltins"):
All builtin commands other than precommand modifiers, even those that
have no options, can be given the argument `--' to terminate option
processing. This indicates that the following words are non-option
arguments, but is otherwise ignored. This is useful in cases where
arguments to the command may begin with `-'. For historical reasons,
most builtin commands also recognize a single `-' in a separate word
for this purpose; note that this is less standard and use of `--' is
recommended.
(Actually the doc might not say exactly that due to a Yodl formatting
error, but that's what it is supposed to say. The part about a single
`-' in a separate word is correct and is what matters here.)
next prev parent reply other threads:[~2015-02-04 3:25 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-02-03 23:12 ${var:1:1:=y} Ray Andrews
2015-02-04 0:18 ` ${var:1:1:=y} Lawrence Velázquez
2015-02-04 0:39 ` ${var:1:1:=y} Ray Andrews
2015-02-04 1:30 ` ${var:1:1:=y} Lawrence Velázquez
2015-02-04 3:25 ` Bart Schaefer [this message]
2015-02-04 3:44 ` ${var:1:1:=y} Eric Cook
2015-02-04 17:10 ` The "-" and "--" options (was Re: ${var:1:1:=y}) Bart Schaefer
2015-02-04 17:42 ` Ray Andrews
2015-02-04 23:47 ` Lawrence Velázquez
2015-02-05 1:31 ` Ray Andrews
2015-02-05 1:52 ` Kurtis Rader
2015-02-05 2:03 ` Ray Andrews
2015-02-05 2:24 ` Bart Schaefer
2015-02-04 19:02 ` Peter Stephenson
2015-02-04 19:53 ` ZyX
2015-02-04 5:16 ` ${var:1:1:=y} Ray Andrews
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=150203192508.ZM2159@torch.brasslantern.com \
--to=schaefer@brasslantern.com \
--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).