Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now
8

Using variable name prefix matching and variable indirection in bash: proj_env_repo_db_username=username proj_env_repo_db_host=host proj_env_repo_db_port=port for variable in "${!proj_env_repo@}"; do export "TF_ENV${variable#proj_env_repo}"="${!variable}" done The loop uses "${!proj_env_repo@}" to generate a list of variable names that share the name ...


5

Adding . to the path means that executable files in the current directory are considered by the shell, with the result that you no longer need to type ./ as a prefix. In your case, since it’s the last entry, such commands will only be executed if nothing else matches before hand; so for example, ls will still run /bin/ls. There are a few reasons why this is ...


3

while IFS='=' read -r name value; do new_name="TF_ENV${name#proj_env_repo}" export "$new_name=$value" unset "$name" done < <( printenv | grep proj_env_repo ) The secret ingredients here: Shell Parameter Expansion IFS='=' read -r name value -- uses the = sign to split the incoming line into the component parts. using a Process Substitution ...


2

You could run both commands with the same shell: TEST=`uuidgen` sh -c 'echo "$TEST" && echo "$TEST"'


1

No, using the symlink is fine in this case and is often preferable. For example, say you have something like: /opt/some-program-2.0/bin/ that contains a set of binaries, you might then create a symbolic link: /opt/some-program -> /opt/some-program-2.0 and add /opt/some-program/bin to the PATH. In the future, when some-program-3.0 comes out, you can ...


1

for v in `printenv | grep proj_env_repo` do eval `echo "$v=\$$v" | sed -e 's/proj_env_repo/TF_ENV/'` unset $v done The echo prints: proj_env_repo_db_username=$proj_env_repo_db_username The sed converts the first instance of proj_env_repo with TF_ENV. TF_ENV_db_username=$proj_env_repo_db_username The eval evaluates that statement. The unset ...


1

thanks to @greycat in the #bash IRC channel on freenode, the bash built-in: compgen -e will print all environment variable names: [root@957f5a5fc6de /]# env HOSTNAME=957f5a5fc6de DISTTAG=f30container PWD=/ FBR=f30 HOME=/root LANG=C.UTF-8 LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:... FGC=f30 TERM=xterm SHLVL=1 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/...


1

I like simple things; this will work for POSIX systems: printenv | sed 's;=.*;;' | sort HOME HOSTNAME PATH PWD SHLVL TERM


Only top voted, non community-wiki answers of a minimum length are eligible