1

I've installed some of my code that needs Perl 5.010 on a CentOS 5.x server using perlbrew and it needs the two lines

source ~/perl5/perlbrew/etc/bashrc

and

perlbrew switch perl-5.10.1

To be executed in the shell before I have perl 5.010 in my /usr/bin/env, so I tried to create the following executable bash script to minimise these two steps to ./setEnv.sh

#!/bin/bash
echo "**setting environment variables - 'perlbrew switch-off' to exit"
SETSOURCE= `source ~/perl5/perlbrew/etc/bashrc`
echo $SETSOURCE
SETPERL= `perlbrew switch perl-5.10.1`
echo $SETPERL
KurzedMetal
  • 12,540
  • 6
  • 39
  • 65
user1439590
  • 321
  • 2
  • 3
  • 8
  • Syntax note: Your assignments do nothing because you have whitespace between the variable assignment and the command substitution. – Todd A. Jacobs Jun 06 '12 at 12:20

2 Answers2

2

A process can't modify its parent environment, so you are doing it wrong since the shebang.

Doing a source in a backtick (subshell) only affects the subshell, and it ends after the command execution.

    $ ### test.sh assign "inside" to TEST
    $ TEST='outside'; echo "$(source test.sh; echo $TEST)" - $TEST
    inside - outside

What you probably want to do is source your setEnv.sh script directly from your shell.

    $ ### test.sh assign "inside" to TEST
    $ TEST='outside'; source test.sh; echo $TEST 
    inside
KurzedMetal
  • 12,540
  • 6
  • 39
  • 65
0

Use the source command without backticks. Just write a line

source ~/perl5/perlbrew/etc/bashrc

in your script. (source has side-effects, which doesn't work when you are in a sub-shell. I'm not even sure you can run source as an external command.)

Christopher Creutzig
  • 8,656
  • 35
  • 45
  • I'd recommend removing the last paragraph and add it as a comment. It's not part of the answer. And probably other SOers/visits don't need a lecture about how to ask questions, even if i totally agree too, upvoting David's comment would work too.. – KurzedMetal Jun 06 '12 at 12:15