Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all global variables? #42

Open
decibel opened this issue Jan 4, 2016 · 7 comments
Open

Remove all global variables? #42

decibel opened this issue Jan 4, 2016 · 7 comments

Comments

@decibel
Copy link
Contributor

decibel commented Jan 4, 2016

Any objection to converting all variables to being local? That would make sourcing the file a lot safer.

At first glance, I think this would mostly amount to passing token to a bunch of functions as an argument, and having them echo it back, though maybe you know of a better way.

I guess the other option is to just declare it as local in parse() and leave it to be set and read by everything else as done today...

@dominictarr
Copy link
Owner

that should be fine - although, I got some pull requests from @aidanhs to make this work with the standard sh (not bash). @aidanhs what are your thoughts?

@decibel
Copy link
Contributor Author

decibel commented Jan 7, 2016

On 1/7/16 12:03 AM, Dominic Tarr wrote:

that should be fine - although, I got some pull requests from @aidanhs
https://github.com/aidanhs to make this work with the standard sh (not
bash). @aidanhs https://github.com/aidanhs what are your thoughts?

Oh, interesting. I guess POSIX sh doesn't support local?

It might be possible to have a $use_local that's either empty or set to
'local', and then instead of doing

local my_var

do

$use_local my_var

Jim Nasby, Data Architect, Blue Treble Consulting, Austin TX
Experts in Analytics, Data Architecture and PostgreSQL
Data in Trouble? Get it in Treble! http://BlueTreble.com

@aidanhs
Copy link
Collaborator

aidanhs commented Jan 8, 2016

local is fine in posix shell (if you're on Ubuntu you can get such a shell by running dash). However, you're not allowed multiple declarations on one line - see https://wiki.ubuntu.com/DashAsBinSh#local

@Artoria2e5
Copy link

Well, not really. local is not a part of POSIX shell command language; dash only added it because it is a nice to have. The no multiple per line thing should be tested with a recent version of dash.

@aidanhs
Copy link
Collaborator

aidanhs commented May 16, 2020

@Artoria2e5 huh, I guess that'll teach me to verify things before I believe them.

I've tested multi-var local on both dash and busybox sh and they both seem fine, so I guess it's probably ok to rely on it.

@Artoria2e5
Copy link

Artoria2e5 commented May 17, 2020

The thing about dash and its busybox derivative is that local and export act a bit like a command: in bash doing local a=$b is perfectly fine, but in dash the $b part undergoes splitting and globbing, so you have to double-quote it to be sure. We do have an unquoted expansion in awk_egrep here.

@mikhirev
Copy link

mikhirev commented Jul 3, 2020

Local variables in POSIX shell? Seriously? It is not in standard, so it is not POSIX. (Well, local is in standard, but only as a reserved word with unspecified behavior.) It is not implemented in ksh93, for example. And ksh93 is used as /bin/sh in Solaris 11. So if you are interested in portability, forget local variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants