bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: String behaviour


From: Michael Witten
Subject: Re: String behaviour
Date: Wed, 28 Mar 2012 21:23:02 +0000

On Fri, Jun 24, 2011 at 10:38, BX <address@hidden> wrote:
> #/bin/bash
> # file1
> import()
> {
>   source file2
> }
>
> import
>
> echo v1=$v1, v2=$v2, v3=$v3
>
> #/bin/bash
> # file2
> v1=v1
> declare -x v2=v2
> declare v3=v3
>
> Run above script by
> $ bash file1
>
> Expected result: v1=v1, v2=v2, v3=v3
> Real result: v1=v1, v2=, v3=

>From the documentation here:

  info '(bash)Shell Functions'

we have:

  When the name of a shell function is used as a
  simple command name, the list of commands
  associated with that function name is executed.
  Shell functions are executed in the current shell
  context; no new process is created to interpret
  them.

  ...

  The BODY of the function is the compound
  command COMPOUND-COMMAND

and from:

  info '(bash)Command Grouping'

we have:

  Placing a list of commands between curly braces
  causes the list to be executed in the current
  shell context.  No subshell is created.

and from:

  info '(bash)Bourne Shell Builtins'

we know that the `source' builtin works as follows:

  Read and execute commands from the FILENAME
  argument in the current shell context

and from:

  info '(bash)Bash Builtins'

we have

  When used in a function, `declare' makes each
  NAME local, as with the `local' command, unless
  the `-g' option is used.

So, basically, the results are as expected: You execute the function
`import', which executes `source file2' in the current shell context,
which creates the variable `v1' in the current shell context, but
creates the variables `v2' and `v3' local to the function being
executed in the current shell context, so that by the echo statement,
only `v1' is defined.

Sincerely,
Michael Witten



reply via email to

[Prev in Thread] Current Thread [Next in Thread]