1

I want to save an output of a piped sequence of commands into a variable on a remote server. I'm trying to do it via ssh like this:

ssh $host "kernel_ver=`sudo yum list kernel --showduplicates | grep 7.$os_rel | tr -s " " | cut -d" " -f2 | head -n1`; echo $kernel_ver"

I'm getting this errors:

Error: No matching Packages to list
ssh: Could not resolve hostname kernel_ver= ; echo : Name or service not known

$os_rel is a variable in my script containing a number.

I tried different syntax like: "$( )" to capture the output and it still doesn't work.

2
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See What topics can I ask about here in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask. Commented Oct 9, 2017 at 14:28
  • The question is 100% on topic. Most bash questions are fine. The ones that aren't are purely interactive questions like "how do I change my keybindings?" or "how do I color my prompt?" Commented Oct 9, 2017 at 15:02

1 Answer 1

1

If you use double quotes to surround everything then the backticks will be evaluated by the local shell rather than the remote one. So will the variable reference $kernel_ver. Use single quotes to disable both.

ssh $host 'kernel_ver=`sudo yum list kernel --showduplicates | grep 7.$os_rel | tr -s " " | cut -d" " -f2 | head -n1`; echo $kernel_ver'

If $os_rel is a local variable that you do want expanded then you'll need to temporarily leave single quotes.

ssh $host 'kernel_ver=`sudo yum list kernel --showduplicates | grep 7.'"$os_rel"' | tr -s " " | cut -d" " -f2 | head -n1`; echo $kernel_ver'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.