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

Re: bash

On Wed, Dec 18, 2002 at 04:45:19PM -0600, Dave Sherohman wrote:
> On Wed, Dec 18, 2002 at 04:01:51AM +0000, Pigeon wrote:
> > How does that one work then?
> Observe:
> $ echo '#!/bin/bash' > foo.sh
> $ echo 'echo "It runs"' >> foo.sh
> $ cat foo.sh 
> #!/bin/bash
> echo "It runs"
> $ ./foo.sh
> bash: ./foo.sh: /bin/bash: bad interpreter: Permission denied
> $ bash foo.sh 
> It runs
> You can run a non-executable script by passing its filename to the
> proper interpreter (in which case the #! line isn't needed, BTW).
> As mentioned earlier, though, the profile is sourced, not executed.
> The difference?
> $ echo 'export BAR=1' > foo.sh
> $ chmod +x foo.sh
> $ ./foo.sh
> $ echo $BAR
> $ source foo.sh
> $ echo $BAR
> 1
> Executing foo.sh started a new shell process and set BAR in that process,
> but had no effect on the shell I gave the command to.  Sourcing it
> caused it to run in the existing shell, so it was able to modify that
> environment.  I'm sure you can see that profiles would be much less
> useful if they were executed instead of sourced...

And there was me thinking in my fuzzy way that that was why they
exported variables instead of just setting them.

Thanks to your very clear reply, I actually thought about that. That
wouldn't work, would it. That would make the variables available to
sub-shells of the one executing the .profile, but not the login shell
that executes the shell that executes the .profile.

I think I've had that wrong for about 15 years. Thank you!


Reply to: