Weird behavior of command "local"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Weird behavior of command "local"
# 1  
Old 11-22-2015
Weird behavior of command "local"

Hi there,

I'm running into a very weird situation. Let's forget about the purpose of my initial script please. I noticed the bug whatever I'm trying to do.

I'm on an old server running bash 3.1.17.

Say we have the following script :
Code:
foo:~# cat /tmp/test
#!/bin/bash
f1() {
        local var
        echo "var is ${var+set}"
}
f2() {
        echo "var is ${var+set}"
}
f1
f2

When I execute it, I get the following result:
Code:
foo:~# bash /tmp/test
var is set
var is

Meaning that the command "local" makes the variable be set even if no value was given to it. This is not the behavior I've seen on any other server.

Can anyone explain what's going on?

Thanks
Santiago

---------- Post updated at 00:39 ---------- Previous update was at 00:19 ----------

I tested my little script on several servers and got the following results :
Code:
bash-3.1.17: var is set
bash-3.2.39: var is set
bash-4.1.5:  var is
bash-4.2.37: var is
bash-4.3.30: var is

Is this a bug of bash-3.x?
Am I not expecting the right behavior from "local" command?
Thanks for your help

Santiago

Last edited by Franklin52; 11-23-2015 at 10:47 AM.. Reason: Adding code tags
# 2  
Old 11-23-2015
Apparently this is a difference between bash-3 and bash-4.
(I reproduced the old behavior with bash-3.2.57 and the new behavior with bash-4.1.)
You are right, local var without a value should be a declaration as in bash-4, rather than a definition as in bash-3.
# 3  
Old 11-23-2015
I'm used to use local in all my bash functions just to make sure I'm not messing with variables from the parent script. My workaround to make scripts compatible with bash-3 and bash-4 is :
Code:
foo:~# cat /tmp/test
#!/bin/bash
f1() {
local var
unset var
echo "var is ${var+set}${var-not set}"
}
f1

Do you see any better idea?
Thx
This User Gave Thanks to chebarbudo For This Post:
# 4  
Old 11-23-2015
That seems to work.
Or use the :+ and :- modifiers in your scripts. They do not distinguish between empty and unset - and that's rarely needed.
Code:
echo "var is ${var:+}${var:-unset or empty}"

# 5  
Old 11-23-2015
Thanks MadeInGermany, I know :+ and :- very well.
In my particular case, I actually need to know if a var is set. Example: In a function, I need to parse an argument and at the same time tell if it matches a regular expression.
Code:
foo:~# cat /tmp/test
#!/bin/bash
f1() {
    local user domain
    unset user domain
    eval $(
        echo "$1" | sed -rn 's/^([a-z0-9.]+)@([a-z0-9.]+)$/user=\1;domain=\2/p'
    )
    if [[ ${user+set} = set ]]; then
        echo "this is an email address. the usename is $user. the domain is $domain"
    else
        echo "$1 is not an email address"
        return 1
    fi
}
f1 "$@"
foo:~# bash /tmp/test me@mail.com
this is an email address. the usename is me. the domain is mail.com
foo:~# bash /tmp/test dummy
dummy is not an email address

Of course this is a bare simple example and it's not accurate. But it tells you my original purpose. In the case of bash-3.x, the script fails if you remove the unset command.

Cheers
Santiago
# 6  
Old 11-24-2015
You gave an example where
Code:
  if [[ ${user:+set} = set ]]; then

gives the same result.
Because your sed never returns user=; the + enforces at least one character.
# 7  
Old 11-24-2015
Ok my bad example, sometimes I need the following...
Code:
foo:~# cat /tmp/test
#!/bin/bash
f1() {
    local file ext
    unset file ext
    eval $(
        echo "$1" | sed -rn 's/^([a-z0-9]+)(\.[a-z0-9]+)?$/file=\1;ext=\2/p'
    )
    if [[ ${ext+set} = set ]]; then
        echo "this is a file${ext:+ with extension }$ext."
    else
        echo "$1 is not a correct filename"
        return 1
    fi
}
f1 "$@"
foo:~# bash /tmp/test wei:rd
wei:rd is not a correct filename
foo:~# bash /tmp/test file
this is a file.
foo:~# bash /tmp/test file.ext
this is a file with extension .ext.

The above example would not work with [[ ${ext:+set} = set ]].
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

2. Shell Programming and Scripting

Commenting out "expr" creates weird behavior

This really puzzles me. The following code gives me the error 'expr: syntax error' when I try to do multi-line comment using here document <<EOF echo "Sum is: `expr $1 + $2`" EOF Even if I explicitly comment out the line containing the expr using "#", the error message would still exist... (3 Replies)
Discussion started by: royalibrahim
3 Replies

3. UNIX for Advanced & Expert Users

Weird "sort" behavior

Hi, I'm trying to sort a text file "test": S12 S_S12 S_S1_12 S15 S_N15 S_N1_15 By "sort test", I get: S12 S15 S_N1_15 S_N15 S_S1_12 S_S12 It seems weird: Comparing Line 2 and Line 3, it must be that '-' is bigger than '1'; however, comparing Line 3 and Line 4, it seems that... (3 Replies)
Discussion started by: intermilan
3 Replies

4. UNIX for Advanced & Expert Users

"╭─ " Character combo in $PATH causes strange autocompletion behavior in zsh

I've posted about this before, but only recently narrowed the problem down to a specific cause. Ok, first of all, the behavior: It occurs when autocompletion brings up its list (not when there is only a single option). Basically, if I were to type, say, cd ~/<TAB> I would get something... (2 Replies)
Discussion started by: marshaul
2 Replies

5. UNIX for Dummies Questions & Answers

Unix "look" Command "File too large" Error Message

I am trying to find lines in a text file larger than 3 Gb that start with a given string. My command looks like this: $ look "string" "/home/patrick/filename.txt" However, this gives me the following message: "look: /home/patrick/filename.txt: File too large" So, I have two... (14 Replies)
Discussion started by: shishong
14 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

"Odd" behavior exiting shell script

Is it normal behavior for a shell script that terminates to terminate its parent shell when executed with the "." option? For example, if I have the example script (we'll name it ex.sh): #!/bin/sh if then echo "Bye." exit 2 fi And I execute it like this: >./ex.sh It... (6 Replies)
Discussion started by: DreamWarrior
6 Replies

8. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

10. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies
Login or Register to Ask a Question