SSH with Command Doesn't Run as an Alias












1















I have the following command to remote into a local server and tail -f the latest log file for an application that I have.



The command works perfectly fine from the command line -



ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'



The problem is that when I make it an alias (or even a function), it evaluates the completion of the ls -1r on my local machine and tries to pass that to the remote machine.



alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"

function latestbotlogs {
ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'
}


What syntax do I need to use such that the entire command gets evaluated on the remote machine that I am accessing via SSH.



Thanks in advance!










share|improve this question





























    1















    I have the following command to remote into a local server and tail -f the latest log file for an application that I have.



    The command works perfectly fine from the command line -



    ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'



    The problem is that when I make it an alias (or even a function), it evaluates the completion of the ls -1r on my local machine and tries to pass that to the remote machine.



    alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"

    function latestbotlogs {
    ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'
    }


    What syntax do I need to use such that the entire command gets evaluated on the remote machine that I am accessing via SSH.



    Thanks in advance!










    share|improve this question



























      1












      1








      1








      I have the following command to remote into a local server and tail -f the latest log file for an application that I have.



      The command works perfectly fine from the command line -



      ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'



      The problem is that when I make it an alias (or even a function), it evaluates the completion of the ls -1r on my local machine and tries to pass that to the remote machine.



      alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"

      function latestbotlogs {
      ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'
      }


      What syntax do I need to use such that the entire command gets evaluated on the remote machine that I am accessing via SSH.



      Thanks in advance!










      share|improve this question
















      I have the following command to remote into a local server and tail -f the latest log file for an application that I have.



      The command works perfectly fine from the command line -



      ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'



      The problem is that when I make it an alias (or even a function), it evaluates the completion of the ls -1r on my local machine and tries to pass that to the remote machine.



      alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"

      function latestbotlogs {
      ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'
      }


      What syntax do I need to use such that the entire command gets evaluated on the remote machine that I am accessing via SSH.



      Thanks in advance!







      shell ssh quoting alias function






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 25 '18 at 18:02









      Jeff Schaller

      43.9k1161141




      43.9k1161141










      asked Oct 25 '18 at 17:43









      mattdondersmattdonders

      1084




      1084






















          3 Answers
          3






          active

          oldest

          votes


















          2














          For the alias you need some escapes



          alias latestbotlogs="ssh user@hostname 'tail -f \$\(ls -1r \~/Development/python/twitter-bot/logs/*.log \| head -1\)'"


          or



          alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''


          The second version is easier, you don't have to think about all the operators you have to quote.



          The function should work as it is.






          share|improve this answer
























          • The second one worked - didn't realize there were different escape characters in alias commands. Thanks so much!

            – mattdonders
            Oct 25 '18 at 18:53











          • It's not different escape characters in the alias command, it's that you need to quote or escape once to assign the alias and have enough quotes or escapes left for the actual command.

            – RalfFriedl
            Oct 25 '18 at 19:57



















          1














          An alternative to getting the somewhat complicated quote-escaping right:



           alias botlogs='ssh user@host "ls -r ~/whatever/*log | head -1 | xargs tail -f"'
          # if (selected) filename contains backslash or quotemark(s)
          # need -d'n' on GNU and I don't know good solution on other
          # this also fails if filename contains whitespace, but so did $( )


          although I concur that the function should work without any hackery and in general functions are more consistent and flexible and just plain better than aliases.



          And PS: when ls output is piped (or redirected) it always uses 1-column format, you don't need -1 here.






          share|improve this answer


























          • This is a great alternative solution as well (and is much easier to read). I always forget about xargs & good to know about the ls piped - thanks so much!

            – mattdonders
            Oct 26 '18 at 13:48



















          0
















          You may set the alias by assigning a string wrapped in single quotes to latestbotlogs, as in:



          alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''


          where the expression ' is used to protect single quotes from the shell and have them concatenated in the resulting command. You can verify that they are preserved:



          $ alias latestbotlogs
          alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''''


          While with your alias:



          alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"


          the command substitution is evaluated immediately, upon definition. This happens because single quotes lose their special meaning when escaped (with ) or enclosed in double quotes ("). (Reference: Quoting in "The Open Group Base Specifications Issue 7, 2018 edition").

          And this is the key: single quotes around the remote command are enough to protect it when the alias is invoked, but do not prevent the enclosed command to be substituted when the alias is created.



          You can also use "'" as an alternative to ':



          alias latestbotlogs='ssh user@hostname '"'"'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"'"


          Or enclose the whole command in double quotes and add some escaping:



          alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"


          In this last form, all the characters that retain a special meaning when double-quoted ($, ` and ) have to be escaped (here we have only a $).



          Your function, instead, should just work.






          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%2f477802%2fssh-with-command-doesnt-run-as-an-alias%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            For the alias you need some escapes



            alias latestbotlogs="ssh user@hostname 'tail -f \$\(ls -1r \~/Development/python/twitter-bot/logs/*.log \| head -1\)'"


            or



            alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''


            The second version is easier, you don't have to think about all the operators you have to quote.



            The function should work as it is.






            share|improve this answer
























            • The second one worked - didn't realize there were different escape characters in alias commands. Thanks so much!

              – mattdonders
              Oct 25 '18 at 18:53











            • It's not different escape characters in the alias command, it's that you need to quote or escape once to assign the alias and have enough quotes or escapes left for the actual command.

              – RalfFriedl
              Oct 25 '18 at 19:57
















            2














            For the alias you need some escapes



            alias latestbotlogs="ssh user@hostname 'tail -f \$\(ls -1r \~/Development/python/twitter-bot/logs/*.log \| head -1\)'"


            or



            alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''


            The second version is easier, you don't have to think about all the operators you have to quote.



            The function should work as it is.






            share|improve this answer
























            • The second one worked - didn't realize there were different escape characters in alias commands. Thanks so much!

              – mattdonders
              Oct 25 '18 at 18:53











            • It's not different escape characters in the alias command, it's that you need to quote or escape once to assign the alias and have enough quotes or escapes left for the actual command.

              – RalfFriedl
              Oct 25 '18 at 19:57














            2












            2








            2







            For the alias you need some escapes



            alias latestbotlogs="ssh user@hostname 'tail -f \$\(ls -1r \~/Development/python/twitter-bot/logs/*.log \| head -1\)'"


            or



            alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''


            The second version is easier, you don't have to think about all the operators you have to quote.



            The function should work as it is.






            share|improve this answer













            For the alias you need some escapes



            alias latestbotlogs="ssh user@hostname 'tail -f \$\(ls -1r \~/Development/python/twitter-bot/logs/*.log \| head -1\)'"


            or



            alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''


            The second version is easier, you don't have to think about all the operators you have to quote.



            The function should work as it is.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 25 '18 at 18:09









            RalfFriedlRalfFriedl

            5,44031025




            5,44031025













            • The second one worked - didn't realize there were different escape characters in alias commands. Thanks so much!

              – mattdonders
              Oct 25 '18 at 18:53











            • It's not different escape characters in the alias command, it's that you need to quote or escape once to assign the alias and have enough quotes or escapes left for the actual command.

              – RalfFriedl
              Oct 25 '18 at 19:57



















            • The second one worked - didn't realize there were different escape characters in alias commands. Thanks so much!

              – mattdonders
              Oct 25 '18 at 18:53











            • It's not different escape characters in the alias command, it's that you need to quote or escape once to assign the alias and have enough quotes or escapes left for the actual command.

              – RalfFriedl
              Oct 25 '18 at 19:57

















            The second one worked - didn't realize there were different escape characters in alias commands. Thanks so much!

            – mattdonders
            Oct 25 '18 at 18:53





            The second one worked - didn't realize there were different escape characters in alias commands. Thanks so much!

            – mattdonders
            Oct 25 '18 at 18:53













            It's not different escape characters in the alias command, it's that you need to quote or escape once to assign the alias and have enough quotes or escapes left for the actual command.

            – RalfFriedl
            Oct 25 '18 at 19:57





            It's not different escape characters in the alias command, it's that you need to quote or escape once to assign the alias and have enough quotes or escapes left for the actual command.

            – RalfFriedl
            Oct 25 '18 at 19:57













            1














            An alternative to getting the somewhat complicated quote-escaping right:



             alias botlogs='ssh user@host "ls -r ~/whatever/*log | head -1 | xargs tail -f"'
            # if (selected) filename contains backslash or quotemark(s)
            # need -d'n' on GNU and I don't know good solution on other
            # this also fails if filename contains whitespace, but so did $( )


            although I concur that the function should work without any hackery and in general functions are more consistent and flexible and just plain better than aliases.



            And PS: when ls output is piped (or redirected) it always uses 1-column format, you don't need -1 here.






            share|improve this answer


























            • This is a great alternative solution as well (and is much easier to read). I always forget about xargs & good to know about the ls piped - thanks so much!

              – mattdonders
              Oct 26 '18 at 13:48
















            1














            An alternative to getting the somewhat complicated quote-escaping right:



             alias botlogs='ssh user@host "ls -r ~/whatever/*log | head -1 | xargs tail -f"'
            # if (selected) filename contains backslash or quotemark(s)
            # need -d'n' on GNU and I don't know good solution on other
            # this also fails if filename contains whitespace, but so did $( )


            although I concur that the function should work without any hackery and in general functions are more consistent and flexible and just plain better than aliases.



            And PS: when ls output is piped (or redirected) it always uses 1-column format, you don't need -1 here.






            share|improve this answer


























            • This is a great alternative solution as well (and is much easier to read). I always forget about xargs & good to know about the ls piped - thanks so much!

              – mattdonders
              Oct 26 '18 at 13:48














            1












            1








            1







            An alternative to getting the somewhat complicated quote-escaping right:



             alias botlogs='ssh user@host "ls -r ~/whatever/*log | head -1 | xargs tail -f"'
            # if (selected) filename contains backslash or quotemark(s)
            # need -d'n' on GNU and I don't know good solution on other
            # this also fails if filename contains whitespace, but so did $( )


            although I concur that the function should work without any hackery and in general functions are more consistent and flexible and just plain better than aliases.



            And PS: when ls output is piped (or redirected) it always uses 1-column format, you don't need -1 here.






            share|improve this answer















            An alternative to getting the somewhat complicated quote-escaping right:



             alias botlogs='ssh user@host "ls -r ~/whatever/*log | head -1 | xargs tail -f"'
            # if (selected) filename contains backslash or quotemark(s)
            # need -d'n' on GNU and I don't know good solution on other
            # this also fails if filename contains whitespace, but so did $( )


            although I concur that the function should work without any hackery and in general functions are more consistent and flexible and just plain better than aliases.



            And PS: when ls output is piped (or redirected) it always uses 1-column format, you don't need -1 here.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 26 '18 at 6:05

























            answered Oct 26 '18 at 5:41









            dave_thompson_085dave_thompson_085

            2,10211111




            2,10211111













            • This is a great alternative solution as well (and is much easier to read). I always forget about xargs & good to know about the ls piped - thanks so much!

              – mattdonders
              Oct 26 '18 at 13:48



















            • This is a great alternative solution as well (and is much easier to read). I always forget about xargs & good to know about the ls piped - thanks so much!

              – mattdonders
              Oct 26 '18 at 13:48

















            This is a great alternative solution as well (and is much easier to read). I always forget about xargs & good to know about the ls piped - thanks so much!

            – mattdonders
            Oct 26 '18 at 13:48





            This is a great alternative solution as well (and is much easier to read). I always forget about xargs & good to know about the ls piped - thanks so much!

            – mattdonders
            Oct 26 '18 at 13:48











            0
















            You may set the alias by assigning a string wrapped in single quotes to latestbotlogs, as in:



            alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''


            where the expression ' is used to protect single quotes from the shell and have them concatenated in the resulting command. You can verify that they are preserved:



            $ alias latestbotlogs
            alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''''


            While with your alias:



            alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"


            the command substitution is evaluated immediately, upon definition. This happens because single quotes lose their special meaning when escaped (with ) or enclosed in double quotes ("). (Reference: Quoting in "The Open Group Base Specifications Issue 7, 2018 edition").

            And this is the key: single quotes around the remote command are enough to protect it when the alias is invoked, but do not prevent the enclosed command to be substituted when the alias is created.



            You can also use "'" as an alternative to ':



            alias latestbotlogs='ssh user@hostname '"'"'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"'"


            Or enclose the whole command in double quotes and add some escaping:



            alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"


            In this last form, all the characters that retain a special meaning when double-quoted ($, ` and ) have to be escaped (here we have only a $).



            Your function, instead, should just work.






            share|improve this answer






























              0
















              You may set the alias by assigning a string wrapped in single quotes to latestbotlogs, as in:



              alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''


              where the expression ' is used to protect single quotes from the shell and have them concatenated in the resulting command. You can verify that they are preserved:



              $ alias latestbotlogs
              alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''''


              While with your alias:



              alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"


              the command substitution is evaluated immediately, upon definition. This happens because single quotes lose their special meaning when escaped (with ) or enclosed in double quotes ("). (Reference: Quoting in "The Open Group Base Specifications Issue 7, 2018 edition").

              And this is the key: single quotes around the remote command are enough to protect it when the alias is invoked, but do not prevent the enclosed command to be substituted when the alias is created.



              You can also use "'" as an alternative to ':



              alias latestbotlogs='ssh user@hostname '"'"'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"'"


              Or enclose the whole command in double quotes and add some escaping:



              alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"


              In this last form, all the characters that retain a special meaning when double-quoted ($, ` and ) have to be escaped (here we have only a $).



              Your function, instead, should just work.






              share|improve this answer




























                0












                0








                0









                You may set the alias by assigning a string wrapped in single quotes to latestbotlogs, as in:



                alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''


                where the expression ' is used to protect single quotes from the shell and have them concatenated in the resulting command. You can verify that they are preserved:



                $ alias latestbotlogs
                alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''''


                While with your alias:



                alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"


                the command substitution is evaluated immediately, upon definition. This happens because single quotes lose their special meaning when escaped (with ) or enclosed in double quotes ("). (Reference: Quoting in "The Open Group Base Specifications Issue 7, 2018 edition").

                And this is the key: single quotes around the remote command are enough to protect it when the alias is invoked, but do not prevent the enclosed command to be substituted when the alias is created.



                You can also use "'" as an alternative to ':



                alias latestbotlogs='ssh user@hostname '"'"'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"'"


                Or enclose the whole command in double quotes and add some escaping:



                alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"


                In this last form, all the characters that retain a special meaning when double-quoted ($, ` and ) have to be escaped (here we have only a $).



                Your function, instead, should just work.






                share|improve this answer

















                You may set the alias by assigning a string wrapped in single quotes to latestbotlogs, as in:



                alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''


                where the expression ' is used to protect single quotes from the shell and have them concatenated in the resulting command. You can verify that they are preserved:



                $ alias latestbotlogs
                alias latestbotlogs='ssh user@hostname '''tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)''''


                While with your alias:



                alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"


                the command substitution is evaluated immediately, upon definition. This happens because single quotes lose their special meaning when escaped (with ) or enclosed in double quotes ("). (Reference: Quoting in "The Open Group Base Specifications Issue 7, 2018 edition").

                And this is the key: single quotes around the remote command are enough to protect it when the alias is invoked, but do not prevent the enclosed command to be substituted when the alias is created.



                You can also use "'" as an alternative to ':



                alias latestbotlogs='ssh user@hostname '"'"'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"'"


                Or enclose the whole command in double quotes and add some escaping:



                alias latestbotlogs="ssh user@hostname 'tail -f $(ls -1r ~/Development/python/twitter-bot/logs/*.log | head -1)'"


                In this last form, all the characters that retain a special meaning when double-quoted ($, ` and ) have to be escaped (here we have only a $).



                Your function, instead, should just work.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 8 at 15:53

























                answered Oct 25 '18 at 18:03









                fra-sanfra-san

                1,8771620




                1,8771620






























                    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%2f477802%2fssh-with-command-doesnt-run-as-an-alias%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

                    How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                    is 'sed' thread safe

                    How to make a Squid Proxy server?