how to keep line feed in result returned by bash command





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















let's say, I can rsh to machine XXX as user foo, then after executing:



$result=$(rsh -l foo XXX "ls");
echo $result;


I found that the line feed is removed in the result, and I can't see the result in a line by line way. so what I should do if I'd like to have the line feed int the returned result?










share|improve this question























  • You should not have a dollar sign in front of the varible name when you assign to it. rsh=$(rsh -l foo XXX "ls"). The terminating semicolons are redundant but harmless.

    – tripleee
    Mar 12 '12 at 8:05




















3















let's say, I can rsh to machine XXX as user foo, then after executing:



$result=$(rsh -l foo XXX "ls");
echo $result;


I found that the line feed is removed in the result, and I can't see the result in a line by line way. so what I should do if I'd like to have the line feed int the returned result?










share|improve this question























  • You should not have a dollar sign in front of the varible name when you assign to it. rsh=$(rsh -l foo XXX "ls"). The terminating semicolons are redundant but harmless.

    – tripleee
    Mar 12 '12 at 8:05
















3












3








3








let's say, I can rsh to machine XXX as user foo, then after executing:



$result=$(rsh -l foo XXX "ls");
echo $result;


I found that the line feed is removed in the result, and I can't see the result in a line by line way. so what I should do if I'd like to have the line feed int the returned result?










share|improve this question














let's say, I can rsh to machine XXX as user foo, then after executing:



$result=$(rsh -l foo XXX "ls");
echo $result;


I found that the line feed is removed in the result, and I can't see the result in a line by line way. so what I should do if I'd like to have the line feed int the returned result?







bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 12 '12 at 7:27









HaiYuan ZhangHaiYuan Zhang

1,51952332




1,51952332













  • You should not have a dollar sign in front of the varible name when you assign to it. rsh=$(rsh -l foo XXX "ls"). The terminating semicolons are redundant but harmless.

    – tripleee
    Mar 12 '12 at 8:05





















  • You should not have a dollar sign in front of the varible name when you assign to it. rsh=$(rsh -l foo XXX "ls"). The terminating semicolons are redundant but harmless.

    – tripleee
    Mar 12 '12 at 8:05



















You should not have a dollar sign in front of the varible name when you assign to it. rsh=$(rsh -l foo XXX "ls"). The terminating semicolons are redundant but harmless.

– tripleee
Mar 12 '12 at 8:05







You should not have a dollar sign in front of the varible name when you assign to it. rsh=$(rsh -l foo XXX "ls"). The terminating semicolons are redundant but harmless.

– tripleee
Mar 12 '12 at 8:05












2 Answers
2






active

oldest

votes


















5














The newline is there; the error is in using echo to examine it (without double quotes around the variable, too!)



result=$(rsh -l foo XXX "ls")
echo "$result"


In trivial cases, you can get away without quotes, but the interesting cases might even contain security issues.



If you are only capturing the standard output of rsh so that you can print it to standard output yourself, this is a useless use of echo






share|improve this answer


























  • Probably also prefer ssh over rsh and don't use ls in scripts.

    – tripleee
    Jan 30 at 11:33



















1














Edit: If you mean the newlines between each file, just make sure to quote the result:



result="$(rsh -l foo XXX "ls");"


This will keep any characters except NUL ($''), which can't be stored in a Bash variable.



If you mean that the trailing newline is lost, then this is the simplest solution I know:



resultx="$(commands...; echo x)"
result="${resultx%x}"





share|improve this answer


























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fsuperuser.com%2fquestions%2f399685%2fhow-to-keep-line-feed-in-result-returned-by-bash-command%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









    5














    The newline is there; the error is in using echo to examine it (without double quotes around the variable, too!)



    result=$(rsh -l foo XXX "ls")
    echo "$result"


    In trivial cases, you can get away without quotes, but the interesting cases might even contain security issues.



    If you are only capturing the standard output of rsh so that you can print it to standard output yourself, this is a useless use of echo






    share|improve this answer


























    • Probably also prefer ssh over rsh and don't use ls in scripts.

      – tripleee
      Jan 30 at 11:33
















    5














    The newline is there; the error is in using echo to examine it (without double quotes around the variable, too!)



    result=$(rsh -l foo XXX "ls")
    echo "$result"


    In trivial cases, you can get away without quotes, but the interesting cases might even contain security issues.



    If you are only capturing the standard output of rsh so that you can print it to standard output yourself, this is a useless use of echo






    share|improve this answer


























    • Probably also prefer ssh over rsh and don't use ls in scripts.

      – tripleee
      Jan 30 at 11:33














    5












    5








    5







    The newline is there; the error is in using echo to examine it (without double quotes around the variable, too!)



    result=$(rsh -l foo XXX "ls")
    echo "$result"


    In trivial cases, you can get away without quotes, but the interesting cases might even contain security issues.



    If you are only capturing the standard output of rsh so that you can print it to standard output yourself, this is a useless use of echo






    share|improve this answer















    The newline is there; the error is in using echo to examine it (without double quotes around the variable, too!)



    result=$(rsh -l foo XXX "ls")
    echo "$result"


    In trivial cases, you can get away without quotes, but the interesting cases might even contain security issues.



    If you are only capturing the standard output of rsh so that you can print it to standard output yourself, this is a useless use of echo







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 30 at 6:23

























    answered Mar 12 '12 at 8:04









    tripleeetripleee

    1,88932130




    1,88932130













    • Probably also prefer ssh over rsh and don't use ls in scripts.

      – tripleee
      Jan 30 at 11:33



















    • Probably also prefer ssh over rsh and don't use ls in scripts.

      – tripleee
      Jan 30 at 11:33

















    Probably also prefer ssh over rsh and don't use ls in scripts.

    – tripleee
    Jan 30 at 11:33





    Probably also prefer ssh over rsh and don't use ls in scripts.

    – tripleee
    Jan 30 at 11:33













    1














    Edit: If you mean the newlines between each file, just make sure to quote the result:



    result="$(rsh -l foo XXX "ls");"


    This will keep any characters except NUL ($''), which can't be stored in a Bash variable.



    If you mean that the trailing newline is lost, then this is the simplest solution I know:



    resultx="$(commands...; echo x)"
    result="${resultx%x}"





    share|improve this answer






























      1














      Edit: If you mean the newlines between each file, just make sure to quote the result:



      result="$(rsh -l foo XXX "ls");"


      This will keep any characters except NUL ($''), which can't be stored in a Bash variable.



      If you mean that the trailing newline is lost, then this is the simplest solution I know:



      resultx="$(commands...; echo x)"
      result="${resultx%x}"





      share|improve this answer




























        1












        1








        1







        Edit: If you mean the newlines between each file, just make sure to quote the result:



        result="$(rsh -l foo XXX "ls");"


        This will keep any characters except NUL ($''), which can't be stored in a Bash variable.



        If you mean that the trailing newline is lost, then this is the simplest solution I know:



        resultx="$(commands...; echo x)"
        result="${resultx%x}"





        share|improve this answer















        Edit: If you mean the newlines between each file, just make sure to quote the result:



        result="$(rsh -l foo XXX "ls");"


        This will keep any characters except NUL ($''), which can't be stored in a Bash variable.



        If you mean that the trailing newline is lost, then this is the simplest solution I know:



        resultx="$(commands...; echo x)"
        result="${resultx%x}"






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 12 '12 at 10:04

























        answered Mar 12 '12 at 9:50









        l0b0l0b0

        5,53832441




        5,53832441






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • 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%2fsuperuser.com%2fquestions%2f399685%2fhow-to-keep-line-feed-in-result-returned-by-bash-command%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

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]