zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH 1/2] FAQ: Add "Why does my bash script report an error when I run it under zsh?".
@ 2020-05-28 20:30 Daniel Shahaf
  2020-05-28 20:30 ` [PATCH 2/2] FAQ (3.1): Update ksh compatibility answer for reserved word typeset Daniel Shahaf
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Shahaf @ 2020-05-28 20:30 UTC (permalink / raw)
  To: zsh-workers

---
Uses emdash() from 45791.

 Etc/FAQ.yo | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 54 insertions(+), 3 deletions(-)

diff --git a/Etc/FAQ.yo b/Etc/FAQ.yo
index d1f8b7d83..9b5e2206a 100644
--- a/Etc/FAQ.yo
+++ b/Etc/FAQ.yo
@@ -129,6 +129,7 @@ Chapter 3:  How to get various things to work
 3.28. How do I edit the input buffer in $EDITOR?
 3.29. Why does `which' output for missing commands go to stdout?
 3.30. Why doesn't the expansion mytt(*.{tex,aux,pdf}) do what I expect?
+3.31. Why does my bash script report an error when I run it under zsh?
 
 Chapter 4:  The mysteries of completion
 4.1. What is completion?
@@ -857,6 +858,7 @@ mytt(compctl)
 
 
 sect(Similarities with bash)
+label(25)
 
   The Bourne-Again Shell, bash, is another enhanced Bourne-like shell;
   the most obvious difference from zsh is that it does not attempt to
@@ -959,9 +961,9 @@ label(31)
   Unless you need strict sh/ksh compatibility, you should ask yourself
   whether you really want this behaviour, as it can produce unexpected
   effects for variables with entirely innocuous embedded spaces.  This
-  can cause horrendous quoting problems when invoking scripts from
-  other shells.  The natural way to produce word-splitting behaviour
-  in zsh is via arrays.  For example,
+  can cause horrendous quoting problems when invoking scripts written
+  for other shells (see link(3.31)(331)).  The natural way to produce
+  word-splitting behaviour in zsh is via arrays.  For example,
   verb(
     set -A array one two three twenty
   )
@@ -2034,6 +2036,55 @@ sect(Why doesn't the expansion mytt(*.{tex,aux,pdf}) do what I expect?)
   parse!
 
 
+sect(Why does my bash script report an error when I run it under zsh?)
+label(331)
+
+  bash and zsh are different programming languages.  They are not
+  interchangeable; programs written for either of these languages will,
+  in general, not run under the other.  (The situation is similar with
+  many other pairs of closely-related languages, such as Python 2 and
+  Python 3; C and C++; and even C89 and C11.)
+
+  So, don't run bash scripts under zsh.  If the scripts were written for
+  bash, run them in bash.  There's absolutely no problem with having
+  mytt(#!/usr/bin/env bash) scripts even if mytt(zsh) is your shell for
+  interactive sessions.
+  
+  In fact, if you've recently changed to zsh, we myem(recommend) that
+  you keep your scripts as mytt(#!/usr/bin/env bash), at least for
+  a while: this would make the change more gradual and flatten your
+  learning curve.  Once you're used to zsh, you can decide for each
+  script whether to port it to zsh or keep it as-is.
+
+  That's the answer for myem(scripts), i.e., for external commands that
+  are located in tt($PATH), or located elsewhere and are executed by
+  giving their path explicitly (as in mytt(ls), mytt(/etc/rc.d/sshd),
+  and mytt(./configure)).  For myem(plugins) emdash() code that is
+  executed within the shell itself, that's loaded via the mytt(.),
+  mytt(source), or mytt(autoload) builtins, added to mytt(.zshrc), or
+  pasted interactively at the shell prompt emdash() the answer is
+  different.
+
+  Since the bash and zsh languages do have a common subset, it is
+  feasible to write non-trivial plugins that would run under either of
+  them, if one is sufficiently familiar with both of them.  However,
+  a difference between bash's behaviour and zsh's does not imply that
+  zsh has a bug.  It myem(might) be a bug in zsh, but it might also be
+  a bug in bash, or simply a difference that isn't a bug in either shell
+  (see link(3.1)(31) for an example).
+
+  When bash and zsh behave differently on the same input, whether zsh's
+  behaviour is a bug does not depend on what bash does on the same
+  input; rather, it depends on what zsh's user manual specifies.
+  (By way of comparison, it's not a bug in Emacs that mytt(:q!) doesn't
+  cause it to exit.)
+
+  If you'd like to run a bash script under zsh, you must port the script
+  properly, reviewing it line by line for differences between the two
+  languages and adjusting the script accordingly, just like you would
+  when translating a book from American English to British English.
+
+
 chapter(The mysteries of completion)
 
 

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

* [PATCH 2/2] FAQ (3.1): Update ksh compatibility answer for reserved word typeset.
  2020-05-28 20:30 [PATCH 1/2] FAQ: Add "Why does my bash script report an error when I run it under zsh?" Daniel Shahaf
@ 2020-05-28 20:30 ` Daniel Shahaf
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Shahaf @ 2020-05-28 20:30 UTC (permalink / raw)
  To: zsh-workers

---
Reported in 45922.  I dropped the sentence about bash because I couldn't
figure out what it was referring to with the term "this behaviour".
bash on my system does word-split arguments that look like assignments
with backticks on the RHS for commands other than «typeset».

 Etc/FAQ.yo | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/Etc/FAQ.yo b/Etc/FAQ.yo
index 9b5e2206a..a327eb514 100644
--- a/Etc/FAQ.yo
+++ b/Etc/FAQ.yo
@@ -981,7 +981,7 @@ label(31)
   been automatic word splitting in scalars, which is a sort of
   uncontrollable poor man's array.
 
-  Note that this happens regardless of the value of the internal field
+  Note that word splitting happens regardless of the value of the internal field
   separator, tt($IFS); in other words, with mytt(IFS=:; foo=a:b; args $foo)
   you get the answer 1.
 
@@ -1013,22 +1013,32 @@ label(31)
   or (entirely equivalent) when mytt(emulate ksh) or mytt(emulate sh) is in
   effect.
 
-  There is one other effect of word splitting which differs between ksh
+  There used to be another effect of word splitting which differed between ksh
   and zsh.  In ksh, the builtin commands that declare parameters such
   as tt(typeset) and tt(export) force word-splitting not to take place
   after on an assignment argument:
   verb(
     typeset param=`echo foo bar`
   )
-  in ksh will create a parameter with value mytt(foo bar), but in zsh will
+  in ksh will create a parameter with value mytt(foo bar).
+  
+  zsh used to
   create a parameter tt(param) with value tt(foo) and a parameter tt(bar)
-  whose value is empty.  Contrast this with a normal assignment (no
+  whose value was empty.  Contrast this with a normal assignment (no
   tt(typeset) or other command in front), which never causes a word split
-  unless you have tt(GLOB_ASSIGN) set.  From zsh version 4.0.2 the option
-  tt(KSH_TYPESET), set automatically in compatibility mode, fixes this
-  problem.  Note that in bash this behaviour occurs with all arguments that
-  look like assignments, whatever the command name; to get this behaviour
-  in zsh you have to set the option tt(MAGIC_EQUAL_SUBST).
+  unless you have tt(GLOB_ASSIGN) set.
+  
+  zsh version 4.0.2 and newer creates a single parameter with value
+  mytt(foo bar), like ksh does, when the option tt(KSH_TYPESET) is set.
+  This option gets set automatically when in ksh compatibility mode.
+
+  zsh 5.1 and newer create a single parameter with value mytt(foo bar) by
+  default, in both compatibility and native modes. The older behaviour
+  can be obtained with mytt(disable -r typeset).
+
+  If the options mytt(MAGIC_EQUAL_SUBST) and mytt(KSH_TYPESET) are both
+  set, arguments that look like assignments will not undergo word
+  splitting, whatever the command name.
 
 sect(In which startup file do I put...?)
 

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

end of thread, other threads:[~2020-05-28 20:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-28 20:30 [PATCH 1/2] FAQ: Add "Why does my bash script report an error when I run it under zsh?" Daniel Shahaf
2020-05-28 20:30 ` [PATCH 2/2] FAQ (3.1): Update ksh compatibility answer for reserved word typeset Daniel Shahaf

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