• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

variable

Remove an environment variable

November 7, 2021

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

Filed Under: Linux Tagged With: env, environment, remove, unset, variable

Bash Variable Empty

August 6, 2021

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

Filed Under: Linux Tagged With: bash, check, empty, unset, variable

Remove Quotes From Strings

July 26, 2020

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

Filed Under: Linux Tagged With: bash, jq, quotes, remove, tr, variable

Bash Variables

June 27, 2020

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.

Filed Under: Linux Tagged With: another, bash, file, source, variable

  • Home
  • About
  • Archives

Copyright © 2023