PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, August 20, 2022

[FIXED] How do I get environment variables in a shell script running on an ECS FARGATE container?

 August 20, 2022     amazon-ecs, aws-fargate, docker, environment-variables, shell     No comments   

Issue

I have an ECS Fargate instance (node-alpine base) running with some environment variables on it (unique to that instance).

Environment Variables on container

I can access them in a python script I run here:

 // python_script.py
    name1 = os.environ['REACT_APP_TEAM_ONE_NAME']
    name2 = os.environ['REACT_APP_TEAM_TWO_NAME']

But this shell script in the same directory does not find them:

#!/bin/sh

TEAM_ONE_NAME=printenv REACT_APP_TEAM_ONE_NAME
TEAM_TWO_NAME=printenv REACT_APP_TEAM_TWO_NAME

Output from shell script: REACT_APP_TEAM_ONE_NAME: not found

I'm not sure if this is an ECS issue, a shell script issue, or something else as I haven't worked with these technologies very much. Thanks for any help.


Solution

To access an environment variable in a shell script, put a dollar sign $ before its name, and generally make sure it's inside a double-quoted string:

TEAM_ONE_NAME="$REACT_APP_TEAM_ONE_NAME"

Shell variables and environment variables are very closely related and you may find it easier to just directly use the environment variable in your script.

The syntax you have attempts to run REACT_APP_TEAM_ONE_NAME as a command (that string, not the value of the corresponding environment variable), setting an environment variable TEAM_ONE_NAME to the value printenv (again, a literal string, not the result of a command) only for the scope of that command execution.



Answered By - David Maze
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing