I am on LinuxMint (have tried CentOS too) and have been trying to use the printenv command for a environment variable which may or may not exist. So I want it to print the value of that name if it is present, else print nothing but return with a exit code 0.
However, if I invoke it with a non-existent environment name, it exits with error code 1. The man pages or the help for printenv doesn't say anything about this behaviour.
Here's how I tried it:
> /usr/bin/printenv foo
That just returns, then I do:
> echo $?
The version of printenv is:
printenv (GNU coreutils) 8.25 Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by David MacKenzie and Richard Mlynarik.
Does anyone have an alternate way to achieve what I'm after? Ultimately, I just need to set a bash variable to contain the value of an environment variable if that environment variable exists, else have that bash variable empty: