Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1060
  • Last Modified:

edit CLASSPATH in .bashrc from schell script

I'm writing a shell script in which I want to append variable to my classpath in my .bashrc file.  Is there an easy way to do this?
0
ICPooreman
Asked:
ICPooreman
  • 3
  • 2
1 Solution
 
cjjcliffordCommented:
If you mean you want the classpath ammended for the lifetime of the script itself, simple:

#!/bin/bash
export CLASSPATH=$CLASSPATH:my.jar
echo classpath is changed to $CLASSPATH

However, if you want to run a script, and after running the script is complete to have the classpath appended, this is less simple...
with the same script example above (saved as eg.sh)

if you ran
./eg.sh
echo classpath is $CLASSPATH

afterwards the CLASSPATH env var. is the same as before (since the environment is only set in current shell, and script runs as its own shell).

instead, "source" the script, rather than running it:

. ./eg.sh

This will execute the "export" in the same shell as it is sourced, hence changing it in the current env.

Hope this helps.
C.
0
 
ICPooremanAuthor Commented:
The first part changes the variable in my script but my commands in the script later are still using the original set in the .bashrc file.  If I do an echo on CLASSPATH the value will have changed however, it doesn't appear to be effecting the later commands in the script.

I'm unfamiliar with what the second option is supposed to be doing but it didn't work either.  It closed down my shell and nothing happened when I ran it.
0
 
cjjcliffordCommented:
Do you have the "export" in place? without the "export" settings to the variable are only for the shell itself, not children - the export makes the change for the current shell's children processes (i.e. the "java" command).

The second approach would exit the command shell if there's an exit...

you can also specify classpath variables for Java, e.g.

#!/bin/bash

export MY_CLASSPATH=$CLASSPATH:my.jar

java -cp $MY_CLASSPATH my.org.HelloWorld

Cheers,
C.

0
 
ICPooremanAuthor Commented:
You're right it was a typo on my side, Sorry thanks for the help
0
 
cjjcliffordCommented:
Glad to be of help!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now