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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:50:45+00:00 2026-05-14T01:50:45+00:00

I’m writing a script to ssh in to a list of machines and compare

  • 0

I’m writing a script to ssh in to a list of machines and compare a variable to another value.. I’ve run into a problem (I have a couple workarounds, but at this point I’m just wondering why this method isn’t working).

VAR=`ssh $i "awk -F: '/^bar/ {print \$2}' /local/foo.txt"`

($i would be a hostname. The hosts are trusted, no password prompt is given)

Example of foo.txt:

foo:123456:abcdef
bar:789012:ghijkl
baz:345678:mnopqr

I’m assuming it’s a problem with quotes, or \’s needed somewhere. I’ve tried several methods (different quoting, using $() instead of “, etc) but can’t seem to get it right. My script is working correctly using the following:

VAR=`ssh $i "grep bar /local/foo.txt" | awk -F: '{print \$2}'`

Like I said, just a curiousity, any response is appreciated.

Forgot to post what my output was: awk is spitting out the whole matched line, not the 2nd section. Playing with the quotes and \’s a bit I seemed to get an error about “{print ” command not found etc, as if there was a new line in there somewhere.

  • 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-14T01:50:45+00:00Added an answer on May 14, 2026 at 1:50 am

    sshd gives your command to bash to execute, so it’ll go through the bash interpreter on the remote side as well as the side you’re executing the script on. Let’s look at your command:

    VAR=`ssh $i "awk -F: '/^bar/ {print \$2}' /local/foo.txt"`
    

    You’ve properly escaped the $2 for your local machine so that the bash script you’re executing does not interpret it. But all the quotes are being removed by the time it gets to awk (I’m not entirely sure why the internal quotes get removed) and it’s executing this:

    awk -F: /^bar/ {print $2} /local/foo.txt
    

    bash on the remote side sees $2 and replaces it with an empty string, leaving you with this:

    awk -F: /^bar/ {print } /local/foo.txt
    

    That’s why it prints the entire line. So how do you fix it? You can escape what bash on the remote side will be executing with a slash, like so:

    VAR1=`ssh $i localhost "echo awk -F: '/^bar/ {print \\\$2}' /local/foo.txt"`
    

    Also, you can just echo the command to see what bash is really executing for you to debug any further problems you come across:

    VAR1=`ssh $i localhost "echo awk -F: '/^bar/ {print \$2}' /local/foo.txt"`
    echo VAR1: $VAR1
    
    Execute it and see this output and see right away that it has removed $2:
    VAR1: awk -F: /^bar/ {print } /local/foo.txt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I have an autohotkey script which looks up a word in a bilingual dictionary
I have been unable to fix a problem with Java Unicode and encoding. The
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am writing an app for my school newspaper, which is run completely online
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am currently running into a problem where an element is coming back from

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.