Consider this:
$ ssh localhost bash -c 'export foo=bar'
terdon@localhost's password:
declare -x DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
declare -x HOME="/home/terdon"
declare -x LOGNAME="terdon"
declare -x MAIL="/var/spool/mail/terdon"
declare -x OLDPWD
declare -x PATH="/usr/bin:/bin:/usr/sbin:/sbin"
declare -x PWD="/home/terdon"
declare -x SHELL="/bin/bash"
declare -x SHLVL="2"
declare -x SSH_CLIENT="::1 55858 22"
declare -x SSH_CONNECTION="::1 55858 ::1 22"
declare -x USER="terdon"
declare -x XDG_RUNTIME_DIR="/run/user/1000"
declare -x XDG_SESSION_ID="c5"
declare -x _="/usr/bin/bash"
Why does exporting a variable within a bash -c
session run via ssh result in that list of declare -x
commands (the list of currently exported variables, as far as I can tell)?
Running the same thing without the bash -c
doesn't do that:
$ ssh localhost 'export foo=bar'
terdon@localhost's password:
$
Nor does it happen if we don't export
:
$ ssh localhost bash -c 'foo=bar'
terdon@localhost's password:
$
I tested this by sshing from one Ubuntu machine to another (both running bash 4.3.11) and on an Arch machine, sshing to itself as shown above (bash version 4.4.5).
What's going on here? Why does exporting a variable inside a bash -c
call produce this output?
export
. Zsh does the same thing. – Stephen Kitt Jan 10 '17 at 13:33export
, I'm trying to understand what's happening. I'll edit to clarify that this only happens when exporting. – terdon Jan 10 '17 at 13:39export
run alone? That I hadn't understood. – terdon Jan 10 '17 at 13:41foo=bar
doesn't appear in the list. – deltab Jan 10 '17 at 17:46