简体   繁体   中英

Passing quoted variables in remote ssh command

I want to grep through a few files for a string. In this example I want to grep for "test 1234"

#!/bin/bash

variable="test 1234"
ssh root@server "grep "$variable" /path/*"

This script doesn't work because test 1234 is passed to the server instead of "test 1234"

How can I fix this?

You need to escape the quotes around $variable in your command:

ssh root@server "grep \"$variable\" /path/*"

As it stands, the variable is expanding but because the quotes aren't escaped, the command is searching for the text test in the files 1234 and /path/*

You should be able to escape the double quotes with a \\ backslash character.

see escape double quote in grep

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM