mainThe GNU Bourne-Again SHell - Support: sr #108673, support local context for $-...

 
 

sr #108673: support local context for $- variable

Submitter:  Scott Moser <smoser>
Submitted:  Thu 23 Oct 2014 08:09:40 PM UTC
   
 
Category:  None Priority:  5 - Normal
Severity:  1 - Wish Status:  Done
Privacy:  Public Assigned to:  None
Open/Closed:  Open Operating System:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Wed 25 Mar 2015 10:39:52 AM UTC, comment #1: 

This is already in bash 4.4:

dualbus@yaqui:~/local/src/gnu/bash$ f(){ local -; set -f; echo $-; }; echo $-; f; echo $-
himBH
fhimBH
himBH
dualbus@yaqui:~/local/src/gnu/bash$
GNU bash, version 4.4.0(2)-devel (x86_64-unknown-linux-gnu)

This report can be closed.

Eduardo Bustamante <dualbus>
Thu 23 Oct 2014 08:09:40 PM UTC, original submission:  

Dash supports local function context for the $- variable.
To my knowledge, bash does not.

the times i've wanted this most was when i wanted to do glob expansion in a function, but not have it generally on.

ie:
$ dash -fc '
  count_txt_files() {
    local f n=0 -;
    set +f;
    echo "during: $-";
    for f in "$1"/*; do [ -f "$f" ] && n=$(($n+1)); done;
    _RET=$n;
  };

  echo before: $-;
  count_txt_files "$HOME" ;
  echo you have "$_RET" text files;
  echo after: $-'

before: f
during:
you have 48 text files
after: f


in bash, i'm not sure of a way to sanely save and restore the shell options.

Running that in bash shows:
before: fhBc
bash: line 2: local: `-': not a valid identifier
during: hBc
you have 48 text files
after: hBc


the 'local' clearly fails, but the result is seen.  the function modified the global settings.

Scott Moser <smoser>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by chet (Updated the item)
  • -email is unavailable- added by dualbus (Posted a comment)
  • -email is unavailable- added by smoser (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2015-11-13 chet StatusNone Done

    Back to the top

    Powered by Savane 3.13-4448.
    Corresponding source code