4 ms·
>Some examples of languages that use dynamic scoping by default are APL, Bash, Latex and Emacs Lisp. bash isn't dynamically scoped by default; it has only glob
by catern 7y ago
>Some examples of languages that use dynamic scoping by default are APL, Bash, Latex and Emacs Lisp.
bash isn't dynamically scoped by default; it has only global scope by default. You have to use "local" to dynamically scope a variable binding.
- kazinator 7y agoThat's like saying classic Lisp isn't dynamically scoped by default; you have to use let instead of just setq.
- catern 7y agoNo. When I set the value of a symbol in a dynamically scoped by default language, it only affects the closest binding for that symbol. When I assign in bash without "local", it affects global scope. There is no way to do the corresponding behavior of "setq" in bash.
- kazinator 7y agoIt so happens that I actually know what I'm talking about in this area, with regard to both languages/families: #!/bin/bash v=xyz f1() { printf "v = %s\n" $v v=clobber } f2() { local v="abc" f1 f1 } f2 f1 f1 Output: v = abc v = clobber v = xyz v = clobber Almost line-for-line analogous Common Lisp program. Defining the variable is mapped to defparameter, local mapped to let and setq to assignment: (defparameter v 'xyz) (defun f1 () (format t "v = ~a~%" v) (setq v 'clobber)) (defun f2 () (let ((v 'abc)) (f1) (f1))) (f2) (f1) (f1) Output (from clisp): v = ABC v = CLOBBER v = XYZ v = CLOBBER
- catern 7y agoThat seems to be the case. My original statement was wrong then. My mistake - for some reason I thought the bash behavior was different.