How to remove an environment variable in Linux.
unset AWS_PROFILE |
You can run env to validate if variable is still present.
env |
cloud engineer
How to remove an environment variable in Linux.
unset AWS_PROFILE |
unset AWS_PROFILE
You can run env to validate if variable is still present.
env |
env
Here’s how to check in Bash if a variable is empty or unset.
if [ -z "${VAR}" ]; then echo 'do something' else echo 'do another' fi |
if [ -z "${VAR}" ]; then echo 'do something' else echo 'do another' fi
One liner
if [ -z "${VAR}" ]; then echo 'do something'; else echo 'do another'; fi |
if [ -z "${VAR}" ]; then echo 'do something'; else echo 'do another'; fi
The inverse
if [ ! -z "${VAR}" ]; then echo 'do something' else echo 'do another' fi |
if [ ! -z "${VAR}" ]; then echo 'do something' else echo 'do another' fi
if [ ! -z "${VAR}" ]; then echo 'do something'; else echo 'do another'; fi |
if [ ! -z "${VAR}" ]; then echo 'do something'; else echo 'do another'; fi
Here’s the command to remove quotes from a string in Bash by using the tr -d command. I’m using jq tool to extract the access key from a json file. Bash returns a string surrounded by quotes. To remove the quotes, just pipe the string to the tr -d command.
jq .AccessKey.AccessKeyId key.json |
jq .AccessKey.AccessKeyId key.json
Result:
"ASDFSDFSDFASDFSDFSDFGHGDF" |
"ASDFSDFSDFASDFSDFSDFGHGDF"
Using tr -d.
jq .AccessKey.AccessKeyId key.json | tr -d \" |
jq .AccessKey.AccessKeyId key.json | tr -d \"
Result:
ASDFSDFSDFASDFSDFSDFGHGDF |
ASDFSDFSDFASDFSDFSDFGHGDF
Here’s how to read Bash variables from another file.
Contents of config.sh
#!/bin/bash var1='thisisvariableone' var2='thisisvariabletwo' |
#!/bin/bash var1='thisisvariableone' var2='thisisvariabletwo'
Call config.sh from another script.
#!/bin/bash source config.sh echo var1 echo var2 |
#!/bin/bash source config.sh echo var1 echo var2
This is extremely helpful if you have multiple scripts using a common variable. You just to update one file instead of multiple files. All the other scripts will read and pick up the same variables from a config file. You can use this to setup your global variables.