How to print input arguments in bash script as is












1















Could you please help, does exist a command in bash to quote input argument?



script ./test.sh:



#!/bin/bash

echo ${1}


./test.sh "It costs $1"
This prints It costs, but how to print it as is It costs $1.



Of course it is possible to quote the argument directly in the command:
./test.sh "It costs $1"
and it prints It costs $1. But how to quote it in the script?



UPDATED: It is possible with single quotes ./test.sh 'It costs $1'










share|improve this question




















  • 1





    Exactly as you just did: echo "It costs ${1}", or with single quotes: echo 'It costs ${1}'.

    – Amadan
    Nov 26 '18 at 3:19






  • 1





    When your script executes, it's already too late. There is nothing your script can do to get the original misquoted $1

    – that other guy
    Nov 26 '18 at 3:20











  • Did you mean to print the an argument, or that the cost is a single dollar? $1 will do the latter. If you run as-is with just ./test.sh you'll get what you're seeing, but try ./test.sh "a buck". For real clarity, read this manual, especially here & here.

    – Paul Hodges
    Nov 26 '18 at 15:25
















1















Could you please help, does exist a command in bash to quote input argument?



script ./test.sh:



#!/bin/bash

echo ${1}


./test.sh "It costs $1"
This prints It costs, but how to print it as is It costs $1.



Of course it is possible to quote the argument directly in the command:
./test.sh "It costs $1"
and it prints It costs $1. But how to quote it in the script?



UPDATED: It is possible with single quotes ./test.sh 'It costs $1'










share|improve this question




















  • 1





    Exactly as you just did: echo "It costs ${1}", or with single quotes: echo 'It costs ${1}'.

    – Amadan
    Nov 26 '18 at 3:19






  • 1





    When your script executes, it's already too late. There is nothing your script can do to get the original misquoted $1

    – that other guy
    Nov 26 '18 at 3:20











  • Did you mean to print the an argument, or that the cost is a single dollar? $1 will do the latter. If you run as-is with just ./test.sh you'll get what you're seeing, but try ./test.sh "a buck". For real clarity, read this manual, especially here & here.

    – Paul Hodges
    Nov 26 '18 at 15:25














1












1








1








Could you please help, does exist a command in bash to quote input argument?



script ./test.sh:



#!/bin/bash

echo ${1}


./test.sh "It costs $1"
This prints It costs, but how to print it as is It costs $1.



Of course it is possible to quote the argument directly in the command:
./test.sh "It costs $1"
and it prints It costs $1. But how to quote it in the script?



UPDATED: It is possible with single quotes ./test.sh 'It costs $1'










share|improve this question
















Could you please help, does exist a command in bash to quote input argument?



script ./test.sh:



#!/bin/bash

echo ${1}


./test.sh "It costs $1"
This prints It costs, but how to print it as is It costs $1.



Of course it is possible to quote the argument directly in the command:
./test.sh "It costs $1"
and it prints It costs $1. But how to quote it in the script?



UPDATED: It is possible with single quotes ./test.sh 'It costs $1'







bash quote






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 3:53







Walrus

















asked Nov 26 '18 at 3:17









WalrusWalrus

7418




7418








  • 1





    Exactly as you just did: echo "It costs ${1}", or with single quotes: echo 'It costs ${1}'.

    – Amadan
    Nov 26 '18 at 3:19






  • 1





    When your script executes, it's already too late. There is nothing your script can do to get the original misquoted $1

    – that other guy
    Nov 26 '18 at 3:20











  • Did you mean to print the an argument, or that the cost is a single dollar? $1 will do the latter. If you run as-is with just ./test.sh you'll get what you're seeing, but try ./test.sh "a buck". For real clarity, read this manual, especially here & here.

    – Paul Hodges
    Nov 26 '18 at 15:25














  • 1





    Exactly as you just did: echo "It costs ${1}", or with single quotes: echo 'It costs ${1}'.

    – Amadan
    Nov 26 '18 at 3:19






  • 1





    When your script executes, it's already too late. There is nothing your script can do to get the original misquoted $1

    – that other guy
    Nov 26 '18 at 3:20











  • Did you mean to print the an argument, or that the cost is a single dollar? $1 will do the latter. If you run as-is with just ./test.sh you'll get what you're seeing, but try ./test.sh "a buck". For real clarity, read this manual, especially here & here.

    – Paul Hodges
    Nov 26 '18 at 15:25








1




1





Exactly as you just did: echo "It costs ${1}", or with single quotes: echo 'It costs ${1}'.

– Amadan
Nov 26 '18 at 3:19





Exactly as you just did: echo "It costs ${1}", or with single quotes: echo 'It costs ${1}'.

– Amadan
Nov 26 '18 at 3:19




1




1





When your script executes, it's already too late. There is nothing your script can do to get the original misquoted $1

– that other guy
Nov 26 '18 at 3:20





When your script executes, it's already too late. There is nothing your script can do to get the original misquoted $1

– that other guy
Nov 26 '18 at 3:20













Did you mean to print the an argument, or that the cost is a single dollar? $1 will do the latter. If you run as-is with just ./test.sh you'll get what you're seeing, but try ./test.sh "a buck". For real clarity, read this manual, especially here & here.

– Paul Hodges
Nov 26 '18 at 15:25





Did you mean to print the an argument, or that the cost is a single dollar? $1 will do the latter. If you run as-is with just ./test.sh you'll get what you're seeing, but try ./test.sh "a buck". For real clarity, read this manual, especially here & here.

– Paul Hodges
Nov 26 '18 at 15:25












2 Answers
2






active

oldest

votes


















2














You may update your program like below to print/display all arguments.



#!/bin/bash

echo "$@"


This will print all the arguments passed while running test.sh. $1 is a variable which substituted with empty string while calling your program test.sh. Inside test.sh, first argument you passed in command line becomes $1, second becomes $2 and so forth.



$@ prints all arguments.






share|improve this answer



















  • 2





    Indeed, it is useful to do man bash and read everything under EXPANSION heading. There is so much good info there that many people never see.

    – Amadan
    Nov 26 '18 at 3:57





















0














I often use:



printf "'%s' "  "$@"


This is an alternative to echo when you want each argument quoted.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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%2fstackoverflow.com%2fquestions%2f53474365%2fhow-to-print-input-arguments-in-bash-script-as-is%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









    2














    You may update your program like below to print/display all arguments.



    #!/bin/bash

    echo "$@"


    This will print all the arguments passed while running test.sh. $1 is a variable which substituted with empty string while calling your program test.sh. Inside test.sh, first argument you passed in command line becomes $1, second becomes $2 and so forth.



    $@ prints all arguments.






    share|improve this answer



















    • 2





      Indeed, it is useful to do man bash and read everything under EXPANSION heading. There is so much good info there that many people never see.

      – Amadan
      Nov 26 '18 at 3:57


















    2














    You may update your program like below to print/display all arguments.



    #!/bin/bash

    echo "$@"


    This will print all the arguments passed while running test.sh. $1 is a variable which substituted with empty string while calling your program test.sh. Inside test.sh, first argument you passed in command line becomes $1, second becomes $2 and so forth.



    $@ prints all arguments.






    share|improve this answer



















    • 2





      Indeed, it is useful to do man bash and read everything under EXPANSION heading. There is so much good info there that many people never see.

      – Amadan
      Nov 26 '18 at 3:57
















    2












    2








    2







    You may update your program like below to print/display all arguments.



    #!/bin/bash

    echo "$@"


    This will print all the arguments passed while running test.sh. $1 is a variable which substituted with empty string while calling your program test.sh. Inside test.sh, first argument you passed in command line becomes $1, second becomes $2 and so forth.



    $@ prints all arguments.






    share|improve this answer













    You may update your program like below to print/display all arguments.



    #!/bin/bash

    echo "$@"


    This will print all the arguments passed while running test.sh. $1 is a variable which substituted with empty string while calling your program test.sh. Inside test.sh, first argument you passed in command line becomes $1, second becomes $2 and so forth.



    $@ prints all arguments.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 26 '18 at 3:28









    Robert RanjanRobert Ranjan

    363410




    363410








    • 2





      Indeed, it is useful to do man bash and read everything under EXPANSION heading. There is so much good info there that many people never see.

      – Amadan
      Nov 26 '18 at 3:57
















    • 2





      Indeed, it is useful to do man bash and read everything under EXPANSION heading. There is so much good info there that many people never see.

      – Amadan
      Nov 26 '18 at 3:57










    2




    2





    Indeed, it is useful to do man bash and read everything under EXPANSION heading. There is so much good info there that many people never see.

    – Amadan
    Nov 26 '18 at 3:57







    Indeed, it is useful to do man bash and read everything under EXPANSION heading. There is so much good info there that many people never see.

    – Amadan
    Nov 26 '18 at 3:57















    0














    I often use:



    printf "'%s' "  "$@"


    This is an alternative to echo when you want each argument quoted.






    share|improve this answer




























      0














      I often use:



      printf "'%s' "  "$@"


      This is an alternative to echo when you want each argument quoted.






      share|improve this answer


























        0












        0








        0







        I often use:



        printf "'%s' "  "$@"


        This is an alternative to echo when you want each argument quoted.






        share|improve this answer













        I often use:



        printf "'%s' "  "$@"


        This is an alternative to echo when you want each argument quoted.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 4:19









        CraigCraig

        5368




        5368






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f53474365%2fhow-to-print-input-arguments-in-bash-script-as-is%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

            Wiesbaden

            Marschland

            Dieringhausen