Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr2807 :: Are bash local variables local?

A lesson on dynamic scope vs lexical scope

<< First, < Previous, , Latest >>

Thumbnail of clacke
Hosted by clacke on Tuesday, 2019-05-07 is flagged as Clean and is released under a CC-BY-SA license.
bash, perl, scope, dynamic scope, lexical scope. 1.
The show is available on the Internet Archive at: https://archive.org/details/hpr2807

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:11:01

Bash Scripting.

This is an open series in which Hacker Public Radio Listeners can share their Bash scripting knowledge and experience with the community. General programming topics and Bash commands are explored along with some tutorials for the complete novice.

https://en.wikipedia.org/wiki/Scope_%28computer_science%29

In hpr2739, Dave talked briefly about local variables. But what are they?

In most modern languages, especially in compiled languages, "local" means that the value of a variable cannot be directly known, by looking up the name, outside the bounds of that function, but that’s not how it works in bash.

Languages like C and Python have lexical scope. Lexical scope means local variables are local in the text. The names are local.

If I’m writing code that is textually located outside the function, I cannot even describe how to access the variables within the function, because myvariable in my function is not the same variable, not the same place, as myvariable in your function.

Languages like Bash and Elisp have dynamic scope. That means local variables are local in time. The names are global.

What happens when you declare a variable local in bash is that the existing value of that variable is stowed away, to be brought back when your function exits.

#!/usr/bin/env bash
function sayscope() {
    echo The scope is $whatsmyscope
}

function globalscope() {
    whatsmyscope=global
}

function dynamicscope() {
    whatsmyscope=dynamic
}

function localscope() {
    local whatsmyscope=local
    sayscope
    dynamicscope
    sayscope
}

globalscope
sayscope
localscope
sayscope
The scope is global
The scope is local
The scope is dynamic
The scope is global

Perl has both, and it calls them local (dynamic scope, like bash) and my (lexical scope):

#!/usr/bin/env perl
use v5.10;

sub sayscope {
    say "Dynamic scope is $whatsmyscope";
}

sub globalscope {
    $whatsmyscope="global";
}

sub dynamicscope {
    $whatsmyscope="dynamic";
}

sub lexicalscope {
    my $whatsmyscope="lexical";
    say "Lexical scope is $whatsmyscope";
    sayscope;
}

sub localscope {
    local $whatsmyscope="local";
    sayscope;
    dynamicscope;
    sayscope;
    lexicalscope;
}

globalscope;
sayscope;
localscope;
sayscope;
Dynamic scope is global
Dynamic scope is local
Dynamic scope is dynamic
Lexical scope is lexical
Dynamic scope is dynamic
Dynamic scope is global

You almost never want to use local in Perl, it’s mostly there for historical reasons — lexical scope is a Perl 5 feature. https://perl.plover.com/local.html covers well the remaining few and narrow exceptions where local might be useful.

As dynamic scope has some valid use, it’s available in some otherwise lexically scoped languages. For example, Common LISP has the special form, and several Schemes and Racket have parameter objects:

To dig fully into the history and flora of dynamic and lexical scope merits another episode.


Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2019-06-06 21:02:23 by Dave Morriss

Thanks for this

I would have commented on the Community News show but couldn't make it due to my audio being messed up!

I really appreciated this episode because it made me realise I was a bit unclear about the issues.

The first language I learnt was Algol 60 (around 1970), and later used Pascal a lot. The Algol course was as a Biology undergraduate where they were trying to make us appreciate how we could use computers in our subject. (This was way before Bioinformatics, so we were mainly writing statistical stuff and learning how to plot results).

Anyway, these languages exposed me to lexical scoping, as you mentioned, and I guess I haven't really reflected on the nuances of dynamic scoping since then.

So, thanks for the eye-opener ;-)

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
Who is the host of this show?
What does HPR mean to you?