Skip to content

Instantly share code, notes, and snippets.

@awwsmm
Last active June 6, 2019 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awwsmm/353577e5d97e135b5411e8cb6af39803 to your computer and use it in GitHub Desktop.
Save awwsmm/353577e5d97e135b5411e8cb6af39803 to your computer and use it in GitHub Desktop.
Get all environment and shell variables
#!/bin/bash
set | grep "^[A-Z]" | sed -e 's/[[:space:]].*//' -e 's/=.*//'
# tested on bash, ksh, tcsh, zsh
# Sources:
# https://askubuntu.com/a/275972/360539
# https://stackoverflow.com/a/20348145/2925434
# https://www.tutorialspoint.com/unix/unix-regular-expressions.htm
# https://stackoverflow.com/a/13202791/2925434
# https://superuser.com/a/112837/728488
# shell-agnostic (bash, csh, fish, ksh, sh, tcsh, zsh) script to determine shell type
# work in progress
# get this shell's variables
set | grep "^[A-Z]" | sed -e 's/[[:space:]].*//' -e 's/=.*//' > ~/.shellvars.tmp
# compare to bash
printf "bash " > ~/.shelllikeness.tmp
comm -12 ~/.shellvars.tmp ~/.bash.uniqvars | wc -l | sed 's/^ *//' | xargs printf >> ~/.shelllikeness.tmp
printf "/" >> ~/.shelllikeness.tmp
wc -l ~/.bash.uniqvars | sed 's/^ *//' | sed 's/ .*$//' >> ~/.shelllikeness.tmp
# compare to fish
printf "fish " >> ~/.shelllikeness.tmp
comm -12 ~/.shellvars.tmp ~/.fish.uniqvars | wc -l | sed 's/^ *//' | xargs printf >> ~/.shelllikeness.tmp
printf "/" >> ~/.shelllikeness.tmp
wc -l ~/.fish.uniqvars | sed 's/^ *//' | sed 's/ .*$//' >> ~/.shelllikeness.tmp
# compare to ksh
printf "ksh " >> ~/.shelllikeness.tmp
comm -12 ~/.shellvars.tmp ~/.ksh.uniqvars | wc -l | sed 's/^ *//' | xargs printf >> ~/.shelllikeness.tmp
printf "/" >> ~/.shelllikeness.tmp
wc -l ~/.ksh.uniqvars | sed 's/^ *//' | sed 's/ .*$//' >> ~/.shelllikeness.tmp
# compare to sh
printf "sh " >> ~/.shelllikeness.tmp
comm -12 ~/.shellvars.tmp ~/.sh.uniqvars | wc -l | sed 's/^ *//' | xargs printf >> ~/.shelllikeness.tmp
printf "/" >> ~/.shelllikeness.tmp
wc -l ~/.sh.uniqvars | sed 's/^ *//' | sed 's/ .*$//' >> ~/.shelllikeness.tmp
# compare to tcsh
printf "tcsh " >> ~/.shelllikeness.tmp
comm -12 ~/.shellvars.tmp ~/.tcsh.uniqvars | wc -l | sed 's/^ *//' | xargs printf >> ~/.shelllikeness.tmp
printf "/" >> ~/.shelllikeness.tmp
wc -l ~/.tcsh.uniqvars | sed 's/^ *//' | sed 's/ .*$//' >> ~/.shelllikeness.tmp
# compare to zsh
printf "zsh " >> ~/.shelllikeness.tmp
comm -12 ~/.shellvars.tmp ~/.zsh.uniqvars | wc -l | sed 's/^ *//' | xargs printf >> ~/.shelllikeness.tmp
printf "/" >> ~/.shelllikeness.tmp
wc -l ~/.zsh.uniqvars | sed 's/^ *//' | sed 's/ .*$//' >> ~/.shelllikeness.tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment