* Negative length parameter expansion @ 2015-04-12 7:27 Thorsten Kampe 2015-04-12 9:33 ` ZyX 0 siblings, 1 reply; 4+ messages in thread From: Thorsten Kampe @ 2015-04-12 7:27 UTC (permalink / raw) To: zsh-users Hi, Bash got negative length parameter expansion (like in `${STRING:11:- 17}`) in version 4.2 and Zsh in 4.3.12. How would I do negative length parameter expansion in Bash or Zsh if my version doesn't support it? Thorsten ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Negative length parameter expansion 2015-04-12 7:27 Negative length parameter expansion Thorsten Kampe @ 2015-04-12 9:33 ` ZyX 2015-04-12 11:43 ` Thorsten Kampe 0 siblings, 1 reply; 4+ messages in thread From: ZyX @ 2015-04-12 9:33 UTC (permalink / raw) To: Thorsten Kampe, zsh-users 12.04.2015, 10:47, "Thorsten Kampe" <thorsten@thorstenkampe.de>: > Hi, > > Bash got negative length parameter expansion (like in `${STRING:11:- > 17}`) in version 4.2 and Zsh in 4.3.12. > > How would I do negative length parameter expansion in Bash or Zsh if > my version doesn't support it? > > Thorsten `$#STRING` is a length of the string. `-17` is `$(( ${#STRING}-17+1 ))`. (`$(( ))` may be not necessary, most likely they will not be necessary in zsh, don’t know about bash). `${#PARAMETER}` form is supported by any POSIX shell (but not `$#PARAMETER` without figure braces). ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Negative length parameter expansion 2015-04-12 9:33 ` ZyX @ 2015-04-12 11:43 ` Thorsten Kampe 2015-04-12 11:54 ` ZyX 0 siblings, 1 reply; 4+ messages in thread From: Thorsten Kampe @ 2015-04-12 11:43 UTC (permalink / raw) To: zsh-users * ZyX (Sun, 12 Apr 2015 12:33:52 +0300) > > 12.04.2015, 10:47, "Thorsten Kampe" <thorsten@thorstenkampe.de>: > > Hi, > > > > Bash got negative length parameter expansion (like in `${STRING:11:- > > 17}`) in version 4.2 and Zsh in 4.3.12. > > > > How would I do negative length parameter expansion in Bash or Zsh if > > my version doesn't support it? > > `$#STRING` is a length of the string. `-17` is `$(( ${#STRING}-17+1 ))`. (`$(( ))` may be not necessary, most likely they will not be necessary in zsh, don’t know about bash). `${#PARAMETER}` form is supported by any POSIX shell (but not `$#PARAMETER` without figure braces). Simple and elegant solution, thanks. Just for correctness: your solution has an off-by-one error (actually an "off by offset" error). Offset starts at 0. You're adding 1 but you would have to subtract 0 (or in general: offset). Following the example from <http://wiki.bash-hackers.org/syntax/pe#negative_length_value> ``` MYSTRING="Be liberal in what you accept, and conservative in what you send" offset=11 length=17 printf "${MYSTRING:$offset:-$length}\n" in what you accept, and conservative length=$((${#MYSTRING} - offset - length)) printf "${MYSTRING:$offset:$length}\n" in what you accept, and conservative ``` Thorsten ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Negative length parameter expansion 2015-04-12 11:43 ` Thorsten Kampe @ 2015-04-12 11:54 ` ZyX 0 siblings, 0 replies; 4+ messages in thread From: ZyX @ 2015-04-12 11:54 UTC (permalink / raw) To: Thorsten Kampe, zsh-users 12.04.2015, 14:45, "Thorsten Kampe" <thorsten@thorstenkampe.de>: > * ZyX (Sun, 12 Apr 2015 12:33:52 +0300) >> 12.04.2015, 10:47, "Thorsten Kampe" <thorsten@thorstenkampe.de>: >>> Hi, >>> >>> Bash got negative length parameter expansion (like in `${STRING:11:- >>> 17}`) in version 4.2 and Zsh in 4.3.12. >>> >>> How would I do negative length parameter expansion in Bash or Zsh if >>> my version doesn't support it? >> `$#STRING` is a length of the string. `-17` is `$(( ${#STRING}-17+1 ))`. (`$(( ))` may be not necessary, most likely they will not be necessary in zsh, don’t know about bash). `${#PARAMETER}` form is supported by any POSIX shell (but not `$#PARAMETER` without figure braces). > > Simple and elegant solution, thanks. > > Just for correctness: your solution has an off-by-one error (actually > an "off by offset" error). Offset starts at 0. You're adding 1 but > you would have to subtract 0 (or in general: offset). Sorry, I was always using ${VAR[start,end]} form. My variant is correct for ${VAR[start,end]} (where `start` starts from one, not from zero), not for ${VAR:offset:length} which I never used (and thus for some reason was under impression that it was the other form of expressing the same thing). Thanks for the correction. > > Following the example from > <http://wiki.bash-hackers.org/syntax/pe#negative_length_value> > > ``` > MYSTRING="Be liberal in what you accept, and conservative in what you > send" > offset=11 > length=17 > > printf "${MYSTRING:$offset:-$length}\n" > in what you accept, and conservative > > length=$((${#MYSTRING} - offset - length)) > printf "${MYSTRING:$offset:$length}\n" > in what you accept, and conservative > ``` > > Thorsten ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-04-12 12:05 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2015-04-12 7:27 Negative length parameter expansion Thorsten Kampe 2015-04-12 9:33 ` ZyX 2015-04-12 11:43 ` Thorsten Kampe 2015-04-12 11:54 ` ZyX
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).