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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:30:25+00:00 2026-06-16T04:30:25+00:00

I am writing a shell script and I need to SSH into a server,

  • 0

I am writing a shell script and I need to SSH into a server, perform some actions, and then exit.

To do this, I am using code such as below:

ssh -t username@server '
cd uploads/; \
tar -xvzf torrent.tar.gz; \
'

However, I need to use a variable like so:

DIR="uploads/";

ssh -t username@server '
cd $DIR; \
tar -xvzf torrent.tar.gz; \
'

This doesn’t seem to work because obviously the cd isn’t being executed until the SSH connection is made, and by then there is no $DIR variable (my guess). However, is there any way I could use a variable?

Perhaps a better question is, is there a better way I could lay out my script to perform actions once the SSH connection is made? I am having to be careful, escaping apostrophes, and at one point I am actually SSHing to another server from inside an SSH connection. This is ugly code!

Edit: Just read that if I use ” instead of ‘, the variable will work. However my question still stands about formatting?

  • 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-16T04:30:27+00:00Added an answer on June 16, 2026 at 4:30 am

    Use double quotes to allow expansion. e.g. try this:

    echo "$DIR"
    

    vs

    echo '$DIR'
    

    and note the result.

    It’s worth wrapping this in a shell script thus:

    #!/bin/bash -x
    # ...
    

    and the -x will output shell expansions etc. It makes life very easy for debugging.

    Rather than using the above means to feed in commands, you can use a heredoc. e.g.

    ssh username@host <<EOF
    ls
    EOF
    

    will execute ‘ls’ on the remote host.

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

Sidebar

Related Questions

I'm writing a shell script to do some web server configuration. I need to
I am writing a shell script (which I suck at) and I need some
I am writing a shell script and need to download patches using ftp. There
I'm writing a shell script and I need to strip FIND ME out of
I am writing a shell script that will perform 2 tasks. One task will
I am writing a shell script to put data into hadoop as soon as
I'm writing a script to import some model objects into the database my django
I'm writing a shell script that makes sure my DNS server is looking. Here's
I'm writing a shell script which will rsync files from remote machines, some linux,
I'm writing a shell script to be used with ssh/git. I want to get

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.