Set Environmental Variables

When working with login credentials, server addresses, etc you can hide them from git and other viewers by saving them as local environmental variables.

Steps:

  1. create a .bash_profile if it does not exist yet.
  2. edit the .bash_profile to add or change the variables
  3. activate the .bash_profile

In a terminal/console:

touch ~/.bash_profile
vim ~/.bash_profile
source ~/.bash_profile

To set a new variable in the .bash_profile:

#aws
export AWS_KEY=$"foobarkey123"
export AWS_SECRET=$"foobarsecret123"

To use environmental variables in Python:

import os
awsKey = os.environ['AWS_KEY']
awsSecret = os.environ['AWS_SECRET']

Split a text file in half

I often have the need to split long text files into smaller chunks, and my need requires they are split by line.

Here's my one liner solution:

f=$"filename"; s=$(wc -l $f | awk '{print $1}'); \
h=$(echo "scale=0;" $(($s/2+1)) | bc -q); \
split -l $h $f "output_"; for file in output_*; \
do mv "$file" "$file.txt"; done

Notes:

  • BC can handle floating point numbers, so scale=0 limits any decimal output.

  • Bash math requires double parentheses.