Force variable expansion on remote ssh server

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















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?










share|improve this question

















  • 1





    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
















0















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?










share|improve this question

















  • 1





    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














0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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













  • 1





    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








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











2 Answers
2






active

oldest

votes


















1














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")





share|improve this answer




















  • 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












  • 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












  • That's better. The @ is certainly more readable than the | adjacent to the .

    – Arjen
    Feb 13 at 22:59


















1














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?






share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    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")





    share|improve this answer




















    • 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












    • 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












    • That's better. The @ is certainly more readable than the | adjacent to the .

      – Arjen
      Feb 13 at 22:59















    1














    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")





    share|improve this answer




















    • 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












    • 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












    • That's better. The @ is certainly more readable than the | adjacent to the .

      – Arjen
      Feb 13 at 22:59













    1












    1








    1







    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")





    share|improve this answer















    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")






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 13 at 23:09

























    answered Feb 13 at 22:35









    ArjenArjen

    686




    686







    • 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












    • 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












    • That's better. The @ is certainly more readable than the | adjacent to the .

      – Arjen
      Feb 13 at 22:59












    • 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












    • 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












    • 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













    1














    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?






    share|improve this answer



























      1














      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?






      share|improve this answer

























        1












        1








        1







        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?






        share|improve this answer













        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?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 13 at 23:29









        glenn jackmanglenn jackman

        52.2k572112




        52.2k572112



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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






            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)