Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am able to assign the DISPLAY variable by running the command line:

export DISPLAY=:2
echo $DISPLAY (returns 2)
hostname (returns opt2)

In my #!/bin/bash script using the same command:

echo $DISPLAY (returns 2)
export DISPLAY=:3 
echo $DISPLAY (returns 3)
hostname (returns opt2)

BUT, when I do echo $DISPLAY after the script is finished I still get "2" as the DISPLAY variable; in other words, the script failed to reassign the DISPLAY variable from "2" to "3".

If I run my bash script a second (third, fourth....) time, I always get:

echo $DISPLAY (returns 2)
export DISPLAY=:3
echo $DISPLAY (returns 3)
hostname (returns opt2)

So, although the script says it assigned the DISPLAY variable to "3", it does not!

How do I make my bash script assign the DISPLAY variable?

share|improve this question
1  
Sebastian, this is the third time I've fixed your code formatting. Please take a minute to read our Editing help, especially the part about indenting code. You can use the preview to see how your post will look like. Also, the editor itself has a nice explanation too. –  slhck Oct 3 '11 at 7:09

1 Answer 1

Exporting variables only affects subshells. You cannot affect parent shells this way. If you want your script to be able to modify variables in the current shell then you must source it.

. ./some_script.sh
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.