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
1
  • Syntax note: Your assignments do nothing because you have whitespace between the variable assignment and the command substitution. Commented Jun 6, 2012 at 12:20

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Why can't I press the enter key and have it insert a line break in your comment form? Chrome/Ubuntu12.04 ?? That's not cool.
@user1439590 because, AFAIK, we can't do multi-line comments.
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.)

1 Comment

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..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.