using 'echo' & 'printf' in bash function callsWhy is printf better than echo?Why does my shell script...

Why do neural networks need so many training examples to perform?

How much mayhem could I cause as a sentient fish?

When can a QA tester start his job?

Is there a feather fall weight limit?

Can my spouse sponsor me for a UK visa if I am unemployed?

If I delete my router's history can my ISP still provide it to my parents?

Is there a Linux system call to create a “view” of a range of a file?

False written accusations not made public - is there law to cover this?

How do I append a character to the end of every line in an Excel cell?

Consequences of lack of rigour

How to deal with an incendiary email that was recalled

Is it a fallacy if someone claims they need an explanation for every word of your argument to the point where they don't understand common terms?

What is the data structure of $@ in shell?

What would the chemical name be for C13H8Cl3NO

Gear reduction on large turbofans

Should I reinstall Linux when changing the laptop's CPU?

Finding a logistic regression model which can achieve zero error on a training set training data for a binary classification problem with two features

How does Leonard in "Memento" remember reading and writing?

Why do cars have plastic shrouds over the engine?

Eww, those bytes are gross

What is the most fuel efficient way out of the Solar System?

Do theoretical physics suggest that gravity is the exchange of gravitons or deformation/bending of spacetime?

Why exactly do action photographers need high fps burst cameras?

Is a new Boolean field better than a null reference when a value can be meaningfully absent?



using 'echo' & 'printf' in bash function calls


Why is printf better than echo?Why does my shell script choke on whitespace or other special characters?Difference between printf and echo in bashBash: Passing command with quoted parameters to functionHow to suppress echo of buggy read functionHow to use a for loop inside an echo statement which is used to print statements into another script in UNIXbash function to execute a command as argumentBash function assign value to passed parameterread a file from Server path - bashrsync using function argsbash function arguments strange behaviourHow to change script dir inside a function?













2















I have a simple shell function call and I'm using echo and printf commands to print the parameter I'm passing. I have noticed the following:





  1. echo is printing the output


  2. printf is not printing the output


Am I missing something here?



check_host(){
# prints output
echo $1

# does not print the output
printf $1

}
check_host $(hostname)









share|improve this question









New contributor




sqlcheckpoint is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • that works :) any explanation on this ?

    – sqlcheckpoint
    3 hours ago











  • See also unix.stackexchange.com/questions/131766/…

    – Jeff Schaller
    3 hours ago
















2















I have a simple shell function call and I'm using echo and printf commands to print the parameter I'm passing. I have noticed the following:





  1. echo is printing the output


  2. printf is not printing the output


Am I missing something here?



check_host(){
# prints output
echo $1

# does not print the output
printf $1

}
check_host $(hostname)









share|improve this question









New contributor




sqlcheckpoint is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • that works :) any explanation on this ?

    – sqlcheckpoint
    3 hours ago











  • See also unix.stackexchange.com/questions/131766/…

    – Jeff Schaller
    3 hours ago














2












2








2








I have a simple shell function call and I'm using echo and printf commands to print the parameter I'm passing. I have noticed the following:





  1. echo is printing the output


  2. printf is not printing the output


Am I missing something here?



check_host(){
# prints output
echo $1

# does not print the output
printf $1

}
check_host $(hostname)









share|improve this question









New contributor




sqlcheckpoint is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I have a simple shell function call and I'm using echo and printf commands to print the parameter I'm passing. I have noticed the following:





  1. echo is printing the output


  2. printf is not printing the output


Am I missing something here?



check_host(){
# prints output
echo $1

# does not print the output
printf $1

}
check_host $(hostname)






bash shell-script






share|improve this question









New contributor




sqlcheckpoint is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




sqlcheckpoint is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 3 hours ago









terdon

131k32258436




131k32258436






New contributor




sqlcheckpoint is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









sqlcheckpointsqlcheckpoint

1134




1134




New contributor




sqlcheckpoint is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





sqlcheckpoint is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






sqlcheckpoint is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • that works :) any explanation on this ?

    – sqlcheckpoint
    3 hours ago











  • See also unix.stackexchange.com/questions/131766/…

    – Jeff Schaller
    3 hours ago



















  • that works :) any explanation on this ?

    – sqlcheckpoint
    3 hours ago











  • See also unix.stackexchange.com/questions/131766/…

    – Jeff Schaller
    3 hours ago

















that works :) any explanation on this ?

– sqlcheckpoint
3 hours ago





that works :) any explanation on this ?

– sqlcheckpoint
3 hours ago













See also unix.stackexchange.com/questions/131766/…

– Jeff Schaller
3 hours ago





See also unix.stackexchange.com/questions/131766/…

– Jeff Schaller
3 hours ago










1 Answer
1






active

oldest

votes


















6














The function you show would print the first argument twice, once with a newline appended, and with no newline at the end of the of second output.



E.g. in an interactive Bash shell, you'd get something like this



user@foo /tmp$ check_host foo
foo
foouser@foo /tmp$


The output from printf is there, just not on a line of its own.



The difference between echo and printf is that echo prints a newline at the end even if you don't ask for it (you can prevent that in Bash by using echo -n), and printf works more like the printf() function in C, in that it only prints what you ask. You'll have to explicitly use n in the printf format string to get the newline.



Note that in general, you'd want to quote those variables and the command substitution to prevent issues with word splitting. This probably isn't a problem with the hostname, but if you have values with whitespace, you'll need it.



So:



check_host() {
echo "$1"
printf "%sn" "$1"
}
check_host "$(hostname)"


Printing arbitrary data with printf should also be done through the %s format specifier as above. Otherwise any % signs in the data would be interpreted by printf.



Also see: Why is printf better than echo?






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


    }
    });






    sqlcheckpoint is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503408%2fusing-echo-printf-in-bash-function-calls%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    The function you show would print the first argument twice, once with a newline appended, and with no newline at the end of the of second output.



    E.g. in an interactive Bash shell, you'd get something like this



    user@foo /tmp$ check_host foo
    foo
    foouser@foo /tmp$


    The output from printf is there, just not on a line of its own.



    The difference between echo and printf is that echo prints a newline at the end even if you don't ask for it (you can prevent that in Bash by using echo -n), and printf works more like the printf() function in C, in that it only prints what you ask. You'll have to explicitly use n in the printf format string to get the newline.



    Note that in general, you'd want to quote those variables and the command substitution to prevent issues with word splitting. This probably isn't a problem with the hostname, but if you have values with whitespace, you'll need it.



    So:



    check_host() {
    echo "$1"
    printf "%sn" "$1"
    }
    check_host "$(hostname)"


    Printing arbitrary data with printf should also be done through the %s format specifier as above. Otherwise any % signs in the data would be interpreted by printf.



    Also see: Why is printf better than echo?






    share|improve this answer




























      6














      The function you show would print the first argument twice, once with a newline appended, and with no newline at the end of the of second output.



      E.g. in an interactive Bash shell, you'd get something like this



      user@foo /tmp$ check_host foo
      foo
      foouser@foo /tmp$


      The output from printf is there, just not on a line of its own.



      The difference between echo and printf is that echo prints a newline at the end even if you don't ask for it (you can prevent that in Bash by using echo -n), and printf works more like the printf() function in C, in that it only prints what you ask. You'll have to explicitly use n in the printf format string to get the newline.



      Note that in general, you'd want to quote those variables and the command substitution to prevent issues with word splitting. This probably isn't a problem with the hostname, but if you have values with whitespace, you'll need it.



      So:



      check_host() {
      echo "$1"
      printf "%sn" "$1"
      }
      check_host "$(hostname)"


      Printing arbitrary data with printf should also be done through the %s format specifier as above. Otherwise any % signs in the data would be interpreted by printf.



      Also see: Why is printf better than echo?






      share|improve this answer


























        6












        6








        6







        The function you show would print the first argument twice, once with a newline appended, and with no newline at the end of the of second output.



        E.g. in an interactive Bash shell, you'd get something like this



        user@foo /tmp$ check_host foo
        foo
        foouser@foo /tmp$


        The output from printf is there, just not on a line of its own.



        The difference between echo and printf is that echo prints a newline at the end even if you don't ask for it (you can prevent that in Bash by using echo -n), and printf works more like the printf() function in C, in that it only prints what you ask. You'll have to explicitly use n in the printf format string to get the newline.



        Note that in general, you'd want to quote those variables and the command substitution to prevent issues with word splitting. This probably isn't a problem with the hostname, but if you have values with whitespace, you'll need it.



        So:



        check_host() {
        echo "$1"
        printf "%sn" "$1"
        }
        check_host "$(hostname)"


        Printing arbitrary data with printf should also be done through the %s format specifier as above. Otherwise any % signs in the data would be interpreted by printf.



        Also see: Why is printf better than echo?






        share|improve this answer













        The function you show would print the first argument twice, once with a newline appended, and with no newline at the end of the of second output.



        E.g. in an interactive Bash shell, you'd get something like this



        user@foo /tmp$ check_host foo
        foo
        foouser@foo /tmp$


        The output from printf is there, just not on a line of its own.



        The difference between echo and printf is that echo prints a newline at the end even if you don't ask for it (you can prevent that in Bash by using echo -n), and printf works more like the printf() function in C, in that it only prints what you ask. You'll have to explicitly use n in the printf format string to get the newline.



        Note that in general, you'd want to quote those variables and the command substitution to prevent issues with word splitting. This probably isn't a problem with the hostname, but if you have values with whitespace, you'll need it.



        So:



        check_host() {
        echo "$1"
        printf "%sn" "$1"
        }
        check_host "$(hostname)"


        Printing arbitrary data with printf should also be done through the %s format specifier as above. Otherwise any % signs in the data would be interpreted by printf.



        Also see: Why is printf better than echo?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 3 hours ago









        ilkkachuilkkachu

        60.1k997169




        60.1k997169






















            sqlcheckpoint is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            sqlcheckpoint is a new contributor. Be nice, and check out our Code of Conduct.













            sqlcheckpoint is a new contributor. Be nice, and check out our Code of Conduct.












            sqlcheckpoint is a new contributor. Be nice, and check out our Code of Conduct.
















            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%2f503408%2fusing-echo-printf-in-bash-function-calls%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

            Benedict Cumberbatch Contingut Inicis Debut professional Premis Filmografia bàsica Premis i...

            Monticle de plataforma Contingut Est de Nord Amèrica Interpretacions Altres cultures Vegeu...

            Escacs Janus Enllaços externs Menú de navegacióEscacs JanusJanusschachBrainKing.comChessV