Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6175171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:52:41+00:00 2026-05-23T23:52:41+00:00

I am running scripts on a remote server from a local server via SSH.

  • 0

I am running scripts on a remote server from a local server via SSH. The script gets copied over using SCP in a first place, then called while being passed some arguments as follows:

scp /path/to/script server.example.org:/another/path/

ssh server.example.org \
MYVAR1=1 \
MYVAR2=2 \
/another/path/script

This works fine and on the remote server, the variables MYVAR1 and MYVAR2 are available with their corresponding value.

The issue is that these scripts are in constant development which requires the SSH command to be changed every-time a variable is renamed, added, or removed.

I’m looking for a way of passing all the local environment variables to the remote script (since MYVAR1 and MYVAR2 are actually local environment variables) which would address the SSH command maintenance issue.

Since MYVAR1=1 \ and MYVAR1=1 \ are lines which follow the env command output I tried replacing them with the actual command as follows:

ssh server.example.org \
`env`
/another/path/script

This seems to work for “simple” env output lines (e.g. SHELL=/bin/bash or LOGNAME=sysadmin), however I get errors for more “complex” output lines (e.g. LS_COLORS=rs=0:di=01;34:ln=01;[...] which gives errors such as -bash: 34:ln=01: command not found ). I can get rid of these errors by unsetting the variables corresponding to those complex output lines before running the SSH command (e.g. unset LS_COLORS, then ssh [...]) however I don’t find this very solution very reliable.

Q: Does anybody know how to pass all the local environment variables to a remote script via SSH?

PS: the local environment variables are not environment variables available on the remote machine so I cannot use this solution.

Update with solution

I ended using sed to format the env command output from VAR=VALUE to VAR="VALUE" (and concatenating all lines in to 1) which prevents bash from interpreting some of the output as commands and fixes my problem.

ssh server.example.org \
`env | sed 's/\([^=]*\)=\(.*\)/\1="\2"/' | tr '\n' ' '` \
"/another/path/script"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:52:42+00:00Added an answer on May 23, 2026 at 11:52 pm

    I happened to read the sshd_config man page unrelated to this and found the option AcceptEnv:

    AcceptEnv
    Specifies what environment variables sent by the client
    will be
    copied into the session’s environ(7). See SendEnv in
    ssh_config(5) for how to configure the client. Note that
    envi-
    ronment passing is only supported for protocol 2.
    Variables are
    specified by name, which may contain the wildcard
    characters *'
    and
    ?’. Multiple environment variables may be separated
    by
    whitespace or spread across multiple AcceptEnv directives.
    Be
    warned that some environment variables could be used to
    bypass
    restricted user environments. For this reason, care
    should be
    taken in the use of this directive. The default is not to
    accept
    any environment variables.

    Maybe you could use this with AcceptEnv: *? I haven’t got a box with sshd handy, but try it out!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to test a perl script in a remote server. I tried running
How can I sync local and remote folders using rsync from inside a php
I have mongodb running on a remote server. I can ssh to the remote
I am running a script on a remote server. I ran the script in
Hey I have a windows server running python CGI scripts and I'm having a
I have a PHP script (running on a Linux server) that ouputs the names
A python script is running two parallel python processes ( created via os.fork() )
I have a bash script that does ssh to a remote machine and executes
I find myself running scripts and copy-pasting the output of these runs into emails
We currently deploy web applications by creating a database and running SQL scripts through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.