I want to print all environment variables on Linux using standard tools and bash. I need to see environment name and masked variable with the last 3 characters visible if variable length is greater than 5 (qwerty
-> ***rty
), and masked value with 1 visible character if it's less than 5 (or equal to 5) (zxc
-> **c
).
Example:
FOO=qwerty
BAR=asdfghjkl
BAZ=zxc
I'm expecting this command to print:
FOO=***rty
BAR=******jkl
BAZ=**c
The length of original value and masked is the same here.
At the moment I tried to implement this using this awk
command, but it's handles only some of these cases by replacing all word with 3 *
and cut the value to the last 3 characters (I'm not really good with awk, so maybe it could be implemented better):
env | awk -F= '{ if(length($2)>5) {printf("%s=***%s\n", $1, substr($2, length($2)-2))} }'