I wish to print out the first two characters from $TEXT












4














I want my program to print out the text that I typed followed by the first two characters of that text. Is there any way to do this?



#!/bin/bash
#
# Get text from user repeatedly
#
echo "Type away..."
while read TEXT
do
echo You typed $TEXT
if [ "$TEXT" = "quit" ] || [ "$TEXT" = "q" ] || [ "$TEXT" = "Q" ] || [ "$TEXT" = "QUIT" ]; then
echo So I quit!
exit 0
fi
done

echo "HELP!"









share|improve this question




















  • 1




    ALthough I don't understand the question entirely. If you type foobar what would you want to see? fo or ar or foobarar?
    – Valentin Bajrami
    Dec 4 '18 at 15:14
















4














I want my program to print out the text that I typed followed by the first two characters of that text. Is there any way to do this?



#!/bin/bash
#
# Get text from user repeatedly
#
echo "Type away..."
while read TEXT
do
echo You typed $TEXT
if [ "$TEXT" = "quit" ] || [ "$TEXT" = "q" ] || [ "$TEXT" = "Q" ] || [ "$TEXT" = "QUIT" ]; then
echo So I quit!
exit 0
fi
done

echo "HELP!"









share|improve this question




















  • 1




    ALthough I don't understand the question entirely. If you type foobar what would you want to see? fo or ar or foobarar?
    – Valentin Bajrami
    Dec 4 '18 at 15:14














4












4








4







I want my program to print out the text that I typed followed by the first two characters of that text. Is there any way to do this?



#!/bin/bash
#
# Get text from user repeatedly
#
echo "Type away..."
while read TEXT
do
echo You typed $TEXT
if [ "$TEXT" = "quit" ] || [ "$TEXT" = "q" ] || [ "$TEXT" = "Q" ] || [ "$TEXT" = "QUIT" ]; then
echo So I quit!
exit 0
fi
done

echo "HELP!"









share|improve this question















I want my program to print out the text that I typed followed by the first two characters of that text. Is there any way to do this?



#!/bin/bash
#
# Get text from user repeatedly
#
echo "Type away..."
while read TEXT
do
echo You typed $TEXT
if [ "$TEXT" = "quit" ] || [ "$TEXT" = "q" ] || [ "$TEXT" = "Q" ] || [ "$TEXT" = "QUIT" ]; then
echo So I quit!
exit 0
fi
done

echo "HELP!"






bash shell-script variable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 '18 at 21:00









wjandrea

466413




466413










asked Dec 4 '18 at 14:56









The Real Fawcett

254




254








  • 1




    ALthough I don't understand the question entirely. If you type foobar what would you want to see? fo or ar or foobarar?
    – Valentin Bajrami
    Dec 4 '18 at 15:14














  • 1




    ALthough I don't understand the question entirely. If you type foobar what would you want to see? fo or ar or foobarar?
    – Valentin Bajrami
    Dec 4 '18 at 15:14








1




1




ALthough I don't understand the question entirely. If you type foobar what would you want to see? fo or ar or foobarar?
– Valentin Bajrami
Dec 4 '18 at 15:14




ALthough I don't understand the question entirely. If you type foobar what would you want to see? fo or ar or foobarar?
– Valentin Bajrami
Dec 4 '18 at 15:14










2 Answers
2






active

oldest

votes


















9














You can get the first characters using notation like ${var:0:N} where var is the variable name and N is number of characters you want. So, for your question, ${TEXT:0:2} should give you the first two characters in TEXT. Example:



TEXT="Some text"
echo "$TEXT: '$TEXT', first 2 chars: '${TEXT:0:2}'"


Output:



$TEXT: 'Some text', first 2 chars: 'So'  


The Linux Documentation Project has a chapter called "Manipulating Strings". The "substring extraction" section has more details about this notation.






share|improve this answer































    4














    Just to provide you a general idea. In your case you can use case... esac construction. An example shown here



    #!/usr/bin/env bash

    call_for_help()
    {
    echo "Please help!" >&2
    }

    if (($# < 1)); then
    call_for_help
    fi

    while read -rp "Type away: " TEXT;
    do
    echo "${TEXT:0:2}"
    case "$TEXT" in
    [qQ] | [Qq]uit)
    echo "You quited"
    exit 1
    ;;
    esac
    done





    share|improve this answer



















    • 1




      (1) I applaud the use of caseesac and the [Ww][Oo][Rr][Dd] notation, but you should probably state that your code is not functionally equivalent to the OP's.  The OP's script will recognize only q, Q, quit and QUIT; yours will recognize those and also Quit, qUIT, quIt, and 11 other variants.  (2) The whole point of saying while read …do instead of while true ... do read … is to have the loop end when the read fails, as happens when it reads an EOF (end of a file, or Ctrl+D on a terminal). … (Cont’d)
      – G-Man
      Dec 4 '18 at 23:34






    • 1




      (Cont’d) …  By injecting the echo statements between the read and the do, you create a loop that cannot be terminated by Ctrl+D (and will spin forever if given input from a file).  (3) So it's adding insult to injury that your code doesn't quit when the user tells it to!  (4) Why are you displaying the last two characters of the input?  (5) Why are you treating the input as an array?  (6) What's this about $OPTARG?
      – G-Man
      Dec 4 '18 at 23:34










    • @G-Man I think your comments are very valid indeed. The last line $OPTARG will of course be valid only when getopts is used. Also I put there 2 options (array var and the other one) since the question wasn't clear what OP wanted. Furthermore, it is indeed pointless to have the variable there in between. I'm going to make some changes so others aren't confused! Thanks for the comments
      – Valentin Bajrami
      Dec 5 '18 at 8:35











    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%2f485910%2fi-wish-to-print-out-the-first-two-characters-from-text%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









    9














    You can get the first characters using notation like ${var:0:N} where var is the variable name and N is number of characters you want. So, for your question, ${TEXT:0:2} should give you the first two characters in TEXT. Example:



    TEXT="Some text"
    echo "$TEXT: '$TEXT', first 2 chars: '${TEXT:0:2}'"


    Output:



    $TEXT: 'Some text', first 2 chars: 'So'  


    The Linux Documentation Project has a chapter called "Manipulating Strings". The "substring extraction" section has more details about this notation.






    share|improve this answer




























      9














      You can get the first characters using notation like ${var:0:N} where var is the variable name and N is number of characters you want. So, for your question, ${TEXT:0:2} should give you the first two characters in TEXT. Example:



      TEXT="Some text"
      echo "$TEXT: '$TEXT', first 2 chars: '${TEXT:0:2}'"


      Output:



      $TEXT: 'Some text', first 2 chars: 'So'  


      The Linux Documentation Project has a chapter called "Manipulating Strings". The "substring extraction" section has more details about this notation.






      share|improve this answer


























        9












        9








        9






        You can get the first characters using notation like ${var:0:N} where var is the variable name and N is number of characters you want. So, for your question, ${TEXT:0:2} should give you the first two characters in TEXT. Example:



        TEXT="Some text"
        echo "$TEXT: '$TEXT', first 2 chars: '${TEXT:0:2}'"


        Output:



        $TEXT: 'Some text', first 2 chars: 'So'  


        The Linux Documentation Project has a chapter called "Manipulating Strings". The "substring extraction" section has more details about this notation.






        share|improve this answer














        You can get the first characters using notation like ${var:0:N} where var is the variable name and N is number of characters you want. So, for your question, ${TEXT:0:2} should give you the first two characters in TEXT. Example:



        TEXT="Some text"
        echo "$TEXT: '$TEXT', first 2 chars: '${TEXT:0:2}'"


        Output:



        $TEXT: 'Some text', first 2 chars: 'So'  


        The Linux Documentation Project has a chapter called "Manipulating Strings". The "substring extraction" section has more details about this notation.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 4 '18 at 20:09









        ilkkachu

        56.3k784156




        56.3k784156










        answered Dec 4 '18 at 15:06









        GreenMatt

        22917




        22917

























            4














            Just to provide you a general idea. In your case you can use case... esac construction. An example shown here



            #!/usr/bin/env bash

            call_for_help()
            {
            echo "Please help!" >&2
            }

            if (($# < 1)); then
            call_for_help
            fi

            while read -rp "Type away: " TEXT;
            do
            echo "${TEXT:0:2}"
            case "$TEXT" in
            [qQ] | [Qq]uit)
            echo "You quited"
            exit 1
            ;;
            esac
            done





            share|improve this answer



















            • 1




              (1) I applaud the use of caseesac and the [Ww][Oo][Rr][Dd] notation, but you should probably state that your code is not functionally equivalent to the OP's.  The OP's script will recognize only q, Q, quit and QUIT; yours will recognize those and also Quit, qUIT, quIt, and 11 other variants.  (2) The whole point of saying while read …do instead of while true ... do read … is to have the loop end when the read fails, as happens when it reads an EOF (end of a file, or Ctrl+D on a terminal). … (Cont’d)
              – G-Man
              Dec 4 '18 at 23:34






            • 1




              (Cont’d) …  By injecting the echo statements between the read and the do, you create a loop that cannot be terminated by Ctrl+D (and will spin forever if given input from a file).  (3) So it's adding insult to injury that your code doesn't quit when the user tells it to!  (4) Why are you displaying the last two characters of the input?  (5) Why are you treating the input as an array?  (6) What's this about $OPTARG?
              – G-Man
              Dec 4 '18 at 23:34










            • @G-Man I think your comments are very valid indeed. The last line $OPTARG will of course be valid only when getopts is used. Also I put there 2 options (array var and the other one) since the question wasn't clear what OP wanted. Furthermore, it is indeed pointless to have the variable there in between. I'm going to make some changes so others aren't confused! Thanks for the comments
              – Valentin Bajrami
              Dec 5 '18 at 8:35
















            4














            Just to provide you a general idea. In your case you can use case... esac construction. An example shown here



            #!/usr/bin/env bash

            call_for_help()
            {
            echo "Please help!" >&2
            }

            if (($# < 1)); then
            call_for_help
            fi

            while read -rp "Type away: " TEXT;
            do
            echo "${TEXT:0:2}"
            case "$TEXT" in
            [qQ] | [Qq]uit)
            echo "You quited"
            exit 1
            ;;
            esac
            done





            share|improve this answer



















            • 1




              (1) I applaud the use of caseesac and the [Ww][Oo][Rr][Dd] notation, but you should probably state that your code is not functionally equivalent to the OP's.  The OP's script will recognize only q, Q, quit and QUIT; yours will recognize those and also Quit, qUIT, quIt, and 11 other variants.  (2) The whole point of saying while read …do instead of while true ... do read … is to have the loop end when the read fails, as happens when it reads an EOF (end of a file, or Ctrl+D on a terminal). … (Cont’d)
              – G-Man
              Dec 4 '18 at 23:34






            • 1




              (Cont’d) …  By injecting the echo statements between the read and the do, you create a loop that cannot be terminated by Ctrl+D (and will spin forever if given input from a file).  (3) So it's adding insult to injury that your code doesn't quit when the user tells it to!  (4) Why are you displaying the last two characters of the input?  (5) Why are you treating the input as an array?  (6) What's this about $OPTARG?
              – G-Man
              Dec 4 '18 at 23:34










            • @G-Man I think your comments are very valid indeed. The last line $OPTARG will of course be valid only when getopts is used. Also I put there 2 options (array var and the other one) since the question wasn't clear what OP wanted. Furthermore, it is indeed pointless to have the variable there in between. I'm going to make some changes so others aren't confused! Thanks for the comments
              – Valentin Bajrami
              Dec 5 '18 at 8:35














            4












            4








            4






            Just to provide you a general idea. In your case you can use case... esac construction. An example shown here



            #!/usr/bin/env bash

            call_for_help()
            {
            echo "Please help!" >&2
            }

            if (($# < 1)); then
            call_for_help
            fi

            while read -rp "Type away: " TEXT;
            do
            echo "${TEXT:0:2}"
            case "$TEXT" in
            [qQ] | [Qq]uit)
            echo "You quited"
            exit 1
            ;;
            esac
            done





            share|improve this answer














            Just to provide you a general idea. In your case you can use case... esac construction. An example shown here



            #!/usr/bin/env bash

            call_for_help()
            {
            echo "Please help!" >&2
            }

            if (($# < 1)); then
            call_for_help
            fi

            while read -rp "Type away: " TEXT;
            do
            echo "${TEXT:0:2}"
            case "$TEXT" in
            [qQ] | [Qq]uit)
            echo "You quited"
            exit 1
            ;;
            esac
            done






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 5 '18 at 8:38

























            answered Dec 4 '18 at 15:11









            Valentin Bajrami

            5,90611627




            5,90611627








            • 1




              (1) I applaud the use of caseesac and the [Ww][Oo][Rr][Dd] notation, but you should probably state that your code is not functionally equivalent to the OP's.  The OP's script will recognize only q, Q, quit and QUIT; yours will recognize those and also Quit, qUIT, quIt, and 11 other variants.  (2) The whole point of saying while read …do instead of while true ... do read … is to have the loop end when the read fails, as happens when it reads an EOF (end of a file, or Ctrl+D on a terminal). … (Cont’d)
              – G-Man
              Dec 4 '18 at 23:34






            • 1




              (Cont’d) …  By injecting the echo statements between the read and the do, you create a loop that cannot be terminated by Ctrl+D (and will spin forever if given input from a file).  (3) So it's adding insult to injury that your code doesn't quit when the user tells it to!  (4) Why are you displaying the last two characters of the input?  (5) Why are you treating the input as an array?  (6) What's this about $OPTARG?
              – G-Man
              Dec 4 '18 at 23:34










            • @G-Man I think your comments are very valid indeed. The last line $OPTARG will of course be valid only when getopts is used. Also I put there 2 options (array var and the other one) since the question wasn't clear what OP wanted. Furthermore, it is indeed pointless to have the variable there in between. I'm going to make some changes so others aren't confused! Thanks for the comments
              – Valentin Bajrami
              Dec 5 '18 at 8:35














            • 1




              (1) I applaud the use of caseesac and the [Ww][Oo][Rr][Dd] notation, but you should probably state that your code is not functionally equivalent to the OP's.  The OP's script will recognize only q, Q, quit and QUIT; yours will recognize those and also Quit, qUIT, quIt, and 11 other variants.  (2) The whole point of saying while read …do instead of while true ... do read … is to have the loop end when the read fails, as happens when it reads an EOF (end of a file, or Ctrl+D on a terminal). … (Cont’d)
              – G-Man
              Dec 4 '18 at 23:34






            • 1




              (Cont’d) …  By injecting the echo statements between the read and the do, you create a loop that cannot be terminated by Ctrl+D (and will spin forever if given input from a file).  (3) So it's adding insult to injury that your code doesn't quit when the user tells it to!  (4) Why are you displaying the last two characters of the input?  (5) Why are you treating the input as an array?  (6) What's this about $OPTARG?
              – G-Man
              Dec 4 '18 at 23:34










            • @G-Man I think your comments are very valid indeed. The last line $OPTARG will of course be valid only when getopts is used. Also I put there 2 options (array var and the other one) since the question wasn't clear what OP wanted. Furthermore, it is indeed pointless to have the variable there in between. I'm going to make some changes so others aren't confused! Thanks for the comments
              – Valentin Bajrami
              Dec 5 '18 at 8:35








            1




            1




            (1) I applaud the use of caseesac and the [Ww][Oo][Rr][Dd] notation, but you should probably state that your code is not functionally equivalent to the OP's.  The OP's script will recognize only q, Q, quit and QUIT; yours will recognize those and also Quit, qUIT, quIt, and 11 other variants.  (2) The whole point of saying while read …do instead of while true ... do read … is to have the loop end when the read fails, as happens when it reads an EOF (end of a file, or Ctrl+D on a terminal). … (Cont’d)
            – G-Man
            Dec 4 '18 at 23:34




            (1) I applaud the use of caseesac and the [Ww][Oo][Rr][Dd] notation, but you should probably state that your code is not functionally equivalent to the OP's.  The OP's script will recognize only q, Q, quit and QUIT; yours will recognize those and also Quit, qUIT, quIt, and 11 other variants.  (2) The whole point of saying while read …do instead of while true ... do read … is to have the loop end when the read fails, as happens when it reads an EOF (end of a file, or Ctrl+D on a terminal). … (Cont’d)
            – G-Man
            Dec 4 '18 at 23:34




            1




            1




            (Cont’d) …  By injecting the echo statements between the read and the do, you create a loop that cannot be terminated by Ctrl+D (and will spin forever if given input from a file).  (3) So it's adding insult to injury that your code doesn't quit when the user tells it to!  (4) Why are you displaying the last two characters of the input?  (5) Why are you treating the input as an array?  (6) What's this about $OPTARG?
            – G-Man
            Dec 4 '18 at 23:34




            (Cont’d) …  By injecting the echo statements between the read and the do, you create a loop that cannot be terminated by Ctrl+D (and will spin forever if given input from a file).  (3) So it's adding insult to injury that your code doesn't quit when the user tells it to!  (4) Why are you displaying the last two characters of the input?  (5) Why are you treating the input as an array?  (6) What's this about $OPTARG?
            – G-Man
            Dec 4 '18 at 23:34












            @G-Man I think your comments are very valid indeed. The last line $OPTARG will of course be valid only when getopts is used. Also I put there 2 options (array var and the other one) since the question wasn't clear what OP wanted. Furthermore, it is indeed pointless to have the variable there in between. I'm going to make some changes so others aren't confused! Thanks for the comments
            – Valentin Bajrami
            Dec 5 '18 at 8:35




            @G-Man I think your comments are very valid indeed. The last line $OPTARG will of course be valid only when getopts is used. Also I put there 2 options (array var and the other one) since the question wasn't clear what OP wanted. Furthermore, it is indeed pointless to have the variable there in between. I'm going to make some changes so others aren't confused! Thanks for the comments
            – Valentin Bajrami
            Dec 5 '18 at 8:35


















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f485910%2fi-wish-to-print-out-the-first-two-characters-from-text%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

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...