0

I have the following file, named set_env_lin.sh:

#!/bin/bash

export SLACK_WEBHOOK_DEV=some_value_1 export SLACK_WEBHOOK_REAL=some_value_2

I followed this guide to make it a bash file, but when y call echo $SLACK_WEBHOOK_REAL, I get no value set.

What am I doing wrong?

HuLu ViCa
  • 125
  • 3
    If you want the exported variables to be visible in the parent shell, the file must be sourced (. set_env_lin.sh or source set_env_lin.sh) rather than simply run – steeldriver Jun 22 '22 at 21:59
  • 1
    When you ./set_env_lin.sh, bash creates a process, sets up redirections, and starts bash in that process to interpret your script. Your script adds the environment variable settings to the process's environment. When your script finishes, the process exits, and it's environment (with your changes) is discarded. Like @steeldriver says, you must source the file. source interprets the script in the current shell. Once the environment variable changes are added to the current shell, they'll be passed on to processes started by the current shell. – waltinator Jun 22 '22 at 22:52
  • @waltinator Might as well make that comment the answer. – ubfan1 Jun 23 '22 at 00:19

1 Answers1

2

When you ./set_env_lin.sh, bash creates a process, sets up redirections, and starts /bin/bash in that process to interpret your script. Your script adds the environment variable settings to the process's environment. When your script finishes, the process exits, and its environment (with your changes) is discarded. Like @steeldriver: says, you must source the file. source interprets the script in the current shell. Once the environment variable changes are added to the current shell, they'll be passed on to processes started by the current shell.

waltinator
  • 36,399