rndusr / torf-cli

CLI tool for creating, reading and editing torrent files
GNU General Public License v3.0
134 stars 13 forks source link

Expand environment variables in the config file #8

Closed marcelpaulo closed 6 years ago

marcelpaulo commented 6 years ago

It might be useful to be able to expand environment values in the config file. For instance, I set the comment for sef-created torrents in a bash script:

COMMENT=$USER@$HOSTNAME

It would be nice to be able to set it in torf's config file, like:

comment = $USER@$HOSTNAME
marcelpaulo commented 6 years ago

Great stuff, thanks !

I've just discovered what it seems like a bash quirk: HOSTNAME is not exported in the environment:

paulo:~$ declare -p HOSTNAME
declare -- HOSTNAME="monk"

but running:

#!/bin/bash
echo $HOSTNAME

it's output, when it shouldn't, since it's not exported in the environment. So, need to export it in .profile, so that torf config can pick it up.

rndusr commented 6 years ago

That's as it should be. Environment variables are only exposed to this process. The export command exists so child processes can inherit your environment.

$ foo=bar
$ zsh -c 'echo $foo'

$ export foo=bar
$ zsh -c 'echo $foo'
bar
marcelpaulo commented 6 years ago

Yes and no, as I have just learned. HOSTNAME is a shell variable, so even though it's not exported in the environment, it's visible in shell scripts, but won't be visible from other programs:

paulo:~$ bash -c 'echo $HOSTNAME'
monk
paulo:~$ perl -e 'print $ENV{HOSTNAME}'
paulo:~$ 
marcelpaulo commented 6 years ago

Whereas USER is not a shell variable, but some process (don't know which, I'd imagine the login manager, lightdm in the case of Xubuntu) sets and exports it:

paulo:~$ bash -c 'echo $USER'
paulo
paulo:~$ perl -e 'print $ENV{USER}, "\n"'
paulo
rndusr commented 6 years ago

It does seem to be a bash idiosyncrasy. Interesting.

$ zsh -c 'echo $HOSTNAME'

$ bash -c 'echo $HOSTNAME'
boing