Created
July 31, 2022 15:30
-
-
Save Kjaer/f12ddf881dc71e7a932d4f76a7ce8109 to your computer and use it in GitHub Desktop.
Bash script knowledge with environment variables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if [[ "${CYPRESS_ACCOUNT_ID:-}" ]] | |
| then | |
| export GRAPHQL_ACCOUNT_ID=$CYPRESS_ACCOUNT_ID && echo $GRAPHQL_ACCOUNT_ID | |
| else | |
| export GRAPHQL_ACCOUNT_ID=$(grep GRAPHQL_ACCOUNT_ID .env.local | cut -d '=' -f2) && echo $GRAPHQL_ACCOUNT_ID | |
| fi | |
| if [[ "${CYPRESS_API_TOKEN:-}" ]] | |
| then | |
| export GRAPHQL_AUTHORIZATION=$CYPRESS_API_TOKEN && echo $GRAPHQL_AUTHORIZATION | |
| else | |
| export GRAPHQL_AUTHORIZATION=$(grep GRAPHQL_AUTHORIZATION .env.local | cut -d '=' -f2) && echo $GRAPHQL_AUTHORIZATION | |
| fi |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am gonna rip this shell script apart and explain how each piece works:
test -zCYPRESS_ACCOUNT_IDis an environment variable. I am expectingCYPRESS_ACCOUNT_IDvariable to be available in the shell process' scope. In order to do that, I am usingtestcommand.testcommand is(from
manpage). You can consider it, asifkeyword in any programming language. It evaluates the given expression and return true/false result.In fact, it does not return true/false, it just for simplifying the explanation. According to man page again,
-zparameter is string length checking argument fortestcommandso this leads us to second part,
"${CYPRESS_ACCOUNT_ID:-}\"Pay attention to
"(quotation) character becausetest -zrequires astringin order to evaluate. Let's keep that detail in our mind and continue,${}. It is called parameter expansion.(https://wiki.bash-hackers.org/syntax/pe).
entityis mentioned above for which the variable itself in our case.It's very similar thing that interpolation of template literals in EcmaScript. But please do not get confused, it is not the same thing.
:-means use given default value, if I may explain over an example:As the example illustrates, in the given parameter expansion block (
${}) given variable is not defined,nullor empty, it uses the given default value. It's a similar concept to function's default parameters.So I hope this piece
"${CYPRESS_ACCOUNT_ID:-}"started to make sense. It's expand theCYPRESS_ACCOUNT_IDvariable and returns the value in it, if there's no value:-kicks in and returns nothing. Because ifCYPRESS_ACCOUNT_IDis not defined, I want nothing as default value, so the final result would be""empty string because I wrapped the entire expression with"quotations marks purposefully.You may ask yourself, why don't we use the
${CYPRESS_ACCOUNT_ID}directly with thetestcommand instead of all the fuss I just explain. Suit yourself:I am in deep appreciation to yarn shell, not throwing error when it's running
"${CYPRESS_ACCOUNT_ID:-}". Because, if I ran the previous command (without:-) in shell, It's like:CONSISTENT!
(life of a frontend is never easy :(( )
&&This is the easiest part. I can describe it like, it lets you run next command if the previous commands run successfully.
Otherwise, it stops and won't continue to the next command.
ACCOUNT_ID=$(grep GRAPHQL_ACCOUNT_ID .env.local | cut -d '=' -f2)ACCOUNT_IDis the interim variable that I am using.$()is again another bash specific operation called Command Substitution (https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html) it basically substitutes given command with its result:If you don't substitute command result but assign directly, you're not able to assign the variable.
Let me continue with
grepcommand.grepis a pattern searcher. It returns the result that match with given pattern on specified source (from man page). Here,GRAPHQL_ACCOUNT_IDis the given pattern and.env.localis the source.cutis like sub string command:-dparameter ofcutis letting you able to specify delimiter. Here is the equal sign=is delimiter.-fmeansfield. It lets you pick which field after cut-out.(from man page)
||Double pipe is the opposite of double ampersand (
&&). It allows running command, if the previous command failed.BONUS:

ACCOUNT_ID=$CYPRESS_ACCOUNT_IDIt's a variable assigning. Simply, it assigns
ACCOUNT_IDtoCYPRESS_ACCOUNT_ID's value (mind the$sign). BecauseCYPRESS_ACCOUNT_IDis defined and present in the terminal process scope.echo $ACCOUNT_IDThis is actually simple but important part. Because So far we chained 4 command and condition. When this condition is met, some value will be assigned to
ACCOUNT_IDand byechoing its value, I can use it later with command substitution:$(yarn codegen:auth.account.id)Bottom line of this entire shell script is mimicing ternary operator.