0

test.sh:

#!/bin/bash
export test_var=1

Run the script, then continue to run the command in the terminal: echo $test_var, I got nothing. Why? Since test_var was exported, I thought I could continue to use the variable in the terminal.

Searene
  • 4,150
  • 8
  • 26
  • 28

2 Answers2

1

export is to allow subshells to inherit the variable, it does nothing to allow a subshell to change a value in the parent.

ubfan1
  • 17,041
  • 4
  • 39
  • 47
1

For it to change your current shell environment, run the script with either

. test.sh

or

source test.sh
jlliagre
  • 5,693
  • 1
  • 24
  • 28
  • Thank you for the info. Though I have used the `source` command for thousands of times, I never thought about it seriously. I just tested it, the current shell will source the variables in the script whether the variable was exported or not. It's really helpful. – Searene Mar 26 '16 at 10:53