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 9004019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:48:22+00:00 2026-06-16T00:48:22+00:00

How can I have both local and remote variable in an ssh command? For

  • 0

How can I have both local and remote variable in an ssh command? For example in the following sample code:

A=3;
ssh host@name "B=3; echo $A; echo $B;"

I have access to A but B is not accessible.

But in the following example:

A=3;
ssh host@name 'B=3; echo $A; echo $B;'

I don’t have A and just B is accessible.

Is there any way that both A and B be accessible?

  • 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-06-16T00:48:23+00:00Added an answer on June 16, 2026 at 12:48 am

    I think this is what you want:

    A=3;
    ssh host@name "B=3; echo $A; echo \$B;"
    

    When you use double-quotes:

    Your shell does auto expansion on variables prefixed with $, so in your first example, when it sees

    ssh host@name "B=3; echo $A; echo $B;"
    

    bash expands it to:

    ssh host@name "B=3; echo 3; echo ;"
    

    and then passes host@name "B=3; echo 3; echo ;" as the argument to ssh. This is because you defined A with A=3, but you never defined B, so $B resolves to the empty string locally.


    When you use single-quotes:

    Everything enclosed by single-quotes are interpreted as string-literals, so when you do:

    ssh host@name 'B=3; echo $A; echo $B;'
    

    the instructions B=3; echo $A; echo $B; will be run once you log in to the remote server. You’ve defined B in the remote shell, but you never told it what A is, so $A will resolve to the empty string.


    So when you use \$, as in the solution:

    \$ means to interpret the $ character literally, so we send literally echo $B as one of the instructions to execute remotely, instead of having bash expand $B locally first. What we end up telling ssh to do is equivalent to this:

    ssh host@name 'B=3; echo 3; echo $B;'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the following EJB (using ejb3): @Stateless(name=Queries) @Remote(Queries.class) @Local(Queries.class) public final class
Who can help me make the following query work... Both tables have the fields
So a FILE stream can have both input and output buffers. You can adjust
I have a basic doubt that, How can we have both CLR's on a
I have three entities. Author Newspaper Dissertation Newspaper and Dissertation both can have one
I have a category/file tree structure. Both categories and files can have parents, so
I have tried this both on Ubuntu Linux and on Windows. I can not
I have a site that can be accessed both through http (http://mysite.com) and https
I have a progaram that can be ran both as a winform, or from
I have two view controllers in a tabbar which can both edit data. Therefore,

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.