-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path08-environment-variables.sh
69 lines (56 loc) · 2.27 KB
/
08-environment-variables.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
echo '
################################################
## Example 8.1: #
## how to print out all environment variables #
################################################
'
# the first way is `env`
env | tail # I piped this to tail just because there are a LOT
# here's another way to get environment variables that only works on Linux:
cat /proc/self/environ | tr '\0' '\n' | tail
echo '
######################################
## Example 8.2: #
## printing out environment variables #
## and shell variables #
#######################################
'
# we access them both the same way: $varname
x="i'm a variable"
echo "$x"
echo "$HOME" # $HOME is an environment variable
echo '
########################################
## Example 8.3: #
## child processes inherit environment #
## variables (but not shell variables) #
########################################
'
# What's going on in this example:
# $ bash -c "some bash code"
# starts a bash child process that runs some bash code
# We're doing that here because it's a simple way to start out a child process
# that prints out its environment variables
export ENVVAR='panda'
bash -c 'echo ENVVAR is: $ENVVAR' # this prints out 'panda'
# we can also print out the environment variable from a Python child process
# (but we do it from a bash child process in the rest of the examples just
# because it's less code)
python -c "import os; print('from Python: ENVVAR=' + os.environ['ENVVAR'])"
# but child processes don't inherit regular shell variables
SHELLVAR='baby seal'
bash -c 'echo SHELLVAR is: $SHELLVAR' # this doesn't print out anything
echo '
#######################################
## Example 8.4: #
## how to set an environment variable #
## for a child process with env #
#######################################
'
env ANIMAL=porcupine bash -c 'echo in this child process, ANIMAL=$ANIMAL'
# ANIMAL doesn't get set in the main process
echo "but in the main process, ANIMAL=$ANIMAL"
# it also works without the 'env'
ANIMAL='banana slug' bash -c 'echo in the second child process, ANIMAL=$ANIMAL'
echo "in the main process, we still have ANIMAL=$ANIMAL"