Shell How do I set global env vars? - Tried: /etc/profile /usr/share/skel/dot.profile $HOME/.shrc

FWIW in Linux I use `/etc/environment`.

I have this line that I have appended to all three of /etc/profile /usr/share/skel/dot.profile $HOME/.shrc:
Bash:
export FOO='bar'

And using a Vagrant box of FreeBSD 14 I can see `FOO` in `env` or just by:
Bash:
$ vagrant ssh
$ printf 'FOO = "%s"\n' "${FOO}"
FOO = "bar"
$ exit
$ vagrant ssh <<< 'printf "FOO = %s\n" "${FOO}"'
FOO = bar

However when running it the usual way with `vagrant -c`:
Bash:
$ vagrant ssh -c 'printf "FOO=%s\n" "${FOO}"'
FOO=

(/etc/profile works for the earlier examples the other files I attempted to work with after running into this issue)

How do I set global variables on FreeBSD? - Happy to execute commands and modify multiple files.

Thanks
 
Ok I've tried this nuclear option, and MY_ROOT_DIR is still not set when I vagrant ssh -c 'printenv MY_ROOT_DIR' (but remains present when I do <<< or manually vagrant ssh and type printenv):
Bash:
printf 'export %s\n' \
  'MY_ROOT_DIR='"'"'/opt/myroot'"'" | su -l root -c 'tee -a /etc/profile > /usr/share/skel/dot.profile'
printf 'setenv %s\n' \
  'MY_ROOT_DIR '"'"'/opt/myroot'"'" | su -l root -c 'tee -a /etc/csh.cshrc >> /etc/csh.login'
printf 'standard:\n\t\t%s\n' \
  ':MY_ROOT_DIR='"'"'/opt/myroot'"'" | su -l root -c 'tee -a /etc/login.conf >/dev/null'
 
How do I set global variables on FreeBSD? - Happy to execute commands and modify multiple files.
When in doubt about how something works on FreeBSD then I highly recommend checking out the handbook. In specific, I'm now referring to chapter 3.1.

You see, the answer to your question is: it depends.

Setting an environment variable is done within a shell, so ... it depends on what shell the accounts are using. Depending on that you can probably get away with using /etc/profile but it depends... I actually use this method myself because on my system the shells for my user/service accounts vary between Bourne and Korn which made /etc/profile a viable choice.

And because I rely on csh for the root user that got automatically exempt from all this; which is exactly what I wanted.
 
Ok understood… sort-of. I've set the var in:
  • /etc/profile
  • /usr/share/skel/dot.profile
  • /etc/login.conf
  • /etc/csh.cshrc
  • /etc/csh.login
  • /etc/login.conf
So far it doesn't persist in vagrant ssh -c. Also exported to ssh config so ssh freebsd_14 'printenv MY_ROOT_DIR' fails but ssh freebsd_14 <<< 'printenv MY_ROOT_DIR' works.
Which other files should I be setting?

Happy to modify 6 more files!
 
I don't have any experience with vagrant. It depends on how it is doing things.

The environment variables are set by the shell. Different shells use different files. It also depends on whether it's a login shell or not.

An interactive login Bash shell, for instance, has a precedent of /usr/local/etc/profile, if it exists, followed by the first of ~/.bash_profile, ~/.bash_login, or ~/.profile that exist. If it is a non-interactive login shell it will try ~/.bashrc. If it is non-interactive it looks to the contents of BASH_ENV unless it is invoked with the name sh in which case it looks to ENV. It is also effected by POSIX mode and effective group id etc.

You need to be familiar with the invocation section of the relevant shell's man page.

All of this to say you need to understand what "vagrant ssh -c" is doing behind the scenes, which user it is leveraging, and what that user's default shell is (assuming vagrant isn't forcing a specifc shell).

Setting variables in lots of files and hoping for the best can lead to unanticipated/unwanted effects.
 
Back
Top