Force variable expansion on remote ssh server

Clash Royale CLAN TAG#URR8PPP
I am trying to replace a string in a file on a remote server:
ssh $login "
replacement=`find . -name file`;
sed -i -e 's/contact/$replacement/g' path/file;
"
but I can't get the content of the $replacement variable to be used by sed. The example above prints $replacement in my file. I also tried
sed -i -e 's/contact/"$replacement"/g' path/file;
but it just prints "$replacement"
What would be the correct syntax?
ssh sed
add a comment |
I am trying to replace a string in a file on a remote server:
ssh $login "
replacement=`find . -name file`;
sed -i -e 's/contact/$replacement/g' path/file;
"
but I can't get the content of the $replacement variable to be used by sed. The example above prints $replacement in my file. I also tried
sed -i -e 's/contact/"$replacement"/g' path/file;
but it just prints "$replacement"
What would be the correct syntax?
ssh sed
1
Probably relevant: Variable expansion and quotes within quotes.
– Kamil Maciorowski
Feb 13 at 22:37
You are using single quotes around the wholesedexpression, including the variable expansion. This will prevent the shell from expanding your variable. Could you confirm that you want to replace the stringcontactin some file with the pathnames of a set of files?
– Kusalananda
Feb 13 at 23:03
add a comment |
I am trying to replace a string in a file on a remote server:
ssh $login "
replacement=`find . -name file`;
sed -i -e 's/contact/$replacement/g' path/file;
"
but I can't get the content of the $replacement variable to be used by sed. The example above prints $replacement in my file. I also tried
sed -i -e 's/contact/"$replacement"/g' path/file;
but it just prints "$replacement"
What would be the correct syntax?
ssh sed
I am trying to replace a string in a file on a remote server:
ssh $login "
replacement=`find . -name file`;
sed -i -e 's/contact/$replacement/g' path/file;
"
but I can't get the content of the $replacement variable to be used by sed. The example above prints $replacement in my file. I also tried
sed -i -e 's/contact/"$replacement"/g' path/file;
but it just prints "$replacement"
What would be the correct syntax?
ssh sed
ssh sed
asked Feb 13 at 21:36
SulliSulli
1112
1112
1
Probably relevant: Variable expansion and quotes within quotes.
– Kamil Maciorowski
Feb 13 at 22:37
You are using single quotes around the wholesedexpression, including the variable expansion. This will prevent the shell from expanding your variable. Could you confirm that you want to replace the stringcontactin some file with the pathnames of a set of files?
– Kusalananda
Feb 13 at 23:03
add a comment |
1
Probably relevant: Variable expansion and quotes within quotes.
– Kamil Maciorowski
Feb 13 at 22:37
You are using single quotes around the wholesedexpression, including the variable expansion. This will prevent the shell from expanding your variable. Could you confirm that you want to replace the stringcontactin some file with the pathnames of a set of files?
– Kusalananda
Feb 13 at 23:03
1
1
Probably relevant: Variable expansion and quotes within quotes.
– Kamil Maciorowski
Feb 13 at 22:37
Probably relevant: Variable expansion and quotes within quotes.
– Kamil Maciorowski
Feb 13 at 22:37
You are using single quotes around the whole
sed expression, including the variable expansion. This will prevent the shell from expanding your variable. Could you confirm that you want to replace the string contact in some file with the pathnames of a set of files?– Kusalananda
Feb 13 at 23:03
You are using single quotes around the whole
sed expression, including the variable expansion. This will prevent the shell from expanding your variable. Could you confirm that you want to replace the string contact in some file with the pathnames of a set of files?– Kusalananda
Feb 13 at 23:03
add a comment |
2 Answers
2
active
oldest
votes
The problem is that in between single quotes $replacement isn't expanded.
In this case sed -i -e "s/contact/$replacement/g" path/file; should work.
Or this:
sed -i -e 's/nothing/'$replacement'/g' path/file;
Example:
$ echo "There's nothing there." > file
$ cat file
There's nothing there.
$ replacement=something
$ sed -i -e 's/nothing/'$replacement'/g' file;
$ cat file
There's something there.
In response to Kusalananda's comment below: If replacement, being a path, contains slashes then you would have to pre-process it before you use it with sed:
replacement=$(sed 's@/@\/@g' <<< "$replacement")
2
Note that$replacementmay be a pathname, i.e. it may (will!) contain slashes. Also, you should demonstrate running this on a remote machine usingssh. Usinglocalhostfor testing would be enough.
– Kusalananda
Feb 13 at 22:36
So then there's a need to pre-process the replacement: replacement=$(sed 's///\//g' <<< "$replacement") or replacement=$(sed 's|/|\/|g' <<< "$replacement")
– Arjen
Feb 13 at 22:53
sedcan use almost any other character as delimiter instead of/in a substitution.sed 's@PATTERN@REPLACEMENT@', for example. The only thing is to make sure it's not a character already in the pattern, or in the replacement.
– Kusalananda
Feb 13 at 22:55
That's better. The @ is certainly more readable than the | adjacent to the .
– Arjen
Feb 13 at 22:59
add a comment |
here documents are a good idea to explore when you're in quoting hell:
ssh "$login" <<'END_REMOTE'
replacement=$(find . -name file)
sed -i -e 's/contact/$replacement/g' path/file
END_REMOTE
The opening keyword for the heredoc is quoted, which means the whole heredoc is single quoted. Easier to read, no?
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500496%2fforce-variable-expansion-on-remote-ssh-server%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that in between single quotes $replacement isn't expanded.
In this case sed -i -e "s/contact/$replacement/g" path/file; should work.
Or this:
sed -i -e 's/nothing/'$replacement'/g' path/file;
Example:
$ echo "There's nothing there." > file
$ cat file
There's nothing there.
$ replacement=something
$ sed -i -e 's/nothing/'$replacement'/g' file;
$ cat file
There's something there.
In response to Kusalananda's comment below: If replacement, being a path, contains slashes then you would have to pre-process it before you use it with sed:
replacement=$(sed 's@/@\/@g' <<< "$replacement")
2
Note that$replacementmay be a pathname, i.e. it may (will!) contain slashes. Also, you should demonstrate running this on a remote machine usingssh. Usinglocalhostfor testing would be enough.
– Kusalananda
Feb 13 at 22:36
So then there's a need to pre-process the replacement: replacement=$(sed 's///\//g' <<< "$replacement") or replacement=$(sed 's|/|\/|g' <<< "$replacement")
– Arjen
Feb 13 at 22:53
sedcan use almost any other character as delimiter instead of/in a substitution.sed 's@PATTERN@REPLACEMENT@', for example. The only thing is to make sure it's not a character already in the pattern, or in the replacement.
– Kusalananda
Feb 13 at 22:55
That's better. The @ is certainly more readable than the | adjacent to the .
– Arjen
Feb 13 at 22:59
add a comment |
The problem is that in between single quotes $replacement isn't expanded.
In this case sed -i -e "s/contact/$replacement/g" path/file; should work.
Or this:
sed -i -e 's/nothing/'$replacement'/g' path/file;
Example:
$ echo "There's nothing there." > file
$ cat file
There's nothing there.
$ replacement=something
$ sed -i -e 's/nothing/'$replacement'/g' file;
$ cat file
There's something there.
In response to Kusalananda's comment below: If replacement, being a path, contains slashes then you would have to pre-process it before you use it with sed:
replacement=$(sed 's@/@\/@g' <<< "$replacement")
2
Note that$replacementmay be a pathname, i.e. it may (will!) contain slashes. Also, you should demonstrate running this on a remote machine usingssh. Usinglocalhostfor testing would be enough.
– Kusalananda
Feb 13 at 22:36
So then there's a need to pre-process the replacement: replacement=$(sed 's///\//g' <<< "$replacement") or replacement=$(sed 's|/|\/|g' <<< "$replacement")
– Arjen
Feb 13 at 22:53
sedcan use almost any other character as delimiter instead of/in a substitution.sed 's@PATTERN@REPLACEMENT@', for example. The only thing is to make sure it's not a character already in the pattern, or in the replacement.
– Kusalananda
Feb 13 at 22:55
That's better. The @ is certainly more readable than the | adjacent to the .
– Arjen
Feb 13 at 22:59
add a comment |
The problem is that in between single quotes $replacement isn't expanded.
In this case sed -i -e "s/contact/$replacement/g" path/file; should work.
Or this:
sed -i -e 's/nothing/'$replacement'/g' path/file;
Example:
$ echo "There's nothing there." > file
$ cat file
There's nothing there.
$ replacement=something
$ sed -i -e 's/nothing/'$replacement'/g' file;
$ cat file
There's something there.
In response to Kusalananda's comment below: If replacement, being a path, contains slashes then you would have to pre-process it before you use it with sed:
replacement=$(sed 's@/@\/@g' <<< "$replacement")
The problem is that in between single quotes $replacement isn't expanded.
In this case sed -i -e "s/contact/$replacement/g" path/file; should work.
Or this:
sed -i -e 's/nothing/'$replacement'/g' path/file;
Example:
$ echo "There's nothing there." > file
$ cat file
There's nothing there.
$ replacement=something
$ sed -i -e 's/nothing/'$replacement'/g' file;
$ cat file
There's something there.
In response to Kusalananda's comment below: If replacement, being a path, contains slashes then you would have to pre-process it before you use it with sed:
replacement=$(sed 's@/@\/@g' <<< "$replacement")
edited Feb 13 at 23:09
answered Feb 13 at 22:35
ArjenArjen
686
686
2
Note that$replacementmay be a pathname, i.e. it may (will!) contain slashes. Also, you should demonstrate running this on a remote machine usingssh. Usinglocalhostfor testing would be enough.
– Kusalananda
Feb 13 at 22:36
So then there's a need to pre-process the replacement: replacement=$(sed 's///\//g' <<< "$replacement") or replacement=$(sed 's|/|\/|g' <<< "$replacement")
– Arjen
Feb 13 at 22:53
sedcan use almost any other character as delimiter instead of/in a substitution.sed 's@PATTERN@REPLACEMENT@', for example. The only thing is to make sure it's not a character already in the pattern, or in the replacement.
– Kusalananda
Feb 13 at 22:55
That's better. The @ is certainly more readable than the | adjacent to the .
– Arjen
Feb 13 at 22:59
add a comment |
2
Note that$replacementmay be a pathname, i.e. it may (will!) contain slashes. Also, you should demonstrate running this on a remote machine usingssh. Usinglocalhostfor testing would be enough.
– Kusalananda
Feb 13 at 22:36
So then there's a need to pre-process the replacement: replacement=$(sed 's///\//g' <<< "$replacement") or replacement=$(sed 's|/|\/|g' <<< "$replacement")
– Arjen
Feb 13 at 22:53
sedcan use almost any other character as delimiter instead of/in a substitution.sed 's@PATTERN@REPLACEMENT@', for example. The only thing is to make sure it's not a character already in the pattern, or in the replacement.
– Kusalananda
Feb 13 at 22:55
That's better. The @ is certainly more readable than the | adjacent to the .
– Arjen
Feb 13 at 22:59
2
2
Note that
$replacement may be a pathname, i.e. it may (will!) contain slashes. Also, you should demonstrate running this on a remote machine using ssh. Using localhost for testing would be enough.– Kusalananda
Feb 13 at 22:36
Note that
$replacement may be a pathname, i.e. it may (will!) contain slashes. Also, you should demonstrate running this on a remote machine using ssh. Using localhost for testing would be enough.– Kusalananda
Feb 13 at 22:36
So then there's a need to pre-process the replacement: replacement=$(sed 's///\//g' <<< "$replacement") or replacement=$(sed 's|/|\/|g' <<< "$replacement")
– Arjen
Feb 13 at 22:53
So then there's a need to pre-process the replacement: replacement=$(sed 's///\//g' <<< "$replacement") or replacement=$(sed 's|/|\/|g' <<< "$replacement")
– Arjen
Feb 13 at 22:53
sed can use almost any other character as delimiter instead of / in a substitution. sed 's@PATTERN@REPLACEMENT@', for example. The only thing is to make sure it's not a character already in the pattern, or in the replacement.– Kusalananda
Feb 13 at 22:55
sed can use almost any other character as delimiter instead of / in a substitution. sed 's@PATTERN@REPLACEMENT@', for example. The only thing is to make sure it's not a character already in the pattern, or in the replacement.– Kusalananda
Feb 13 at 22:55
That's better. The @ is certainly more readable than the | adjacent to the .
– Arjen
Feb 13 at 22:59
That's better. The @ is certainly more readable than the | adjacent to the .
– Arjen
Feb 13 at 22:59
add a comment |
here documents are a good idea to explore when you're in quoting hell:
ssh "$login" <<'END_REMOTE'
replacement=$(find . -name file)
sed -i -e 's/contact/$replacement/g' path/file
END_REMOTE
The opening keyword for the heredoc is quoted, which means the whole heredoc is single quoted. Easier to read, no?
add a comment |
here documents are a good idea to explore when you're in quoting hell:
ssh "$login" <<'END_REMOTE'
replacement=$(find . -name file)
sed -i -e 's/contact/$replacement/g' path/file
END_REMOTE
The opening keyword for the heredoc is quoted, which means the whole heredoc is single quoted. Easier to read, no?
add a comment |
here documents are a good idea to explore when you're in quoting hell:
ssh "$login" <<'END_REMOTE'
replacement=$(find . -name file)
sed -i -e 's/contact/$replacement/g' path/file
END_REMOTE
The opening keyword for the heredoc is quoted, which means the whole heredoc is single quoted. Easier to read, no?
here documents are a good idea to explore when you're in quoting hell:
ssh "$login" <<'END_REMOTE'
replacement=$(find . -name file)
sed -i -e 's/contact/$replacement/g' path/file
END_REMOTE
The opening keyword for the heredoc is quoted, which means the whole heredoc is single quoted. Easier to read, no?
answered Feb 13 at 23:29
glenn jackmanglenn jackman
52.2k572112
52.2k572112
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500496%2fforce-variable-expansion-on-remote-ssh-server%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Probably relevant: Variable expansion and quotes within quotes.
– Kamil Maciorowski
Feb 13 at 22:37
You are using single quotes around the whole
sedexpression, including the variable expansion. This will prevent the shell from expanding your variable. Could you confirm that you want to replace the stringcontactin some file with the pathnames of a set of files?– Kusalananda
Feb 13 at 23:03