Grabbing the first [x] characters for a string from a pipe

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
47
down vote

favorite
7












If I have really long output from a command (single line) but I know I only want the first [x] (let's say 8) characters of the output, what's the easiest way to get that? There aren't any delimiters.










share|improve this question























  • related: stackoverflow.com/questions/1405611/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Aug 7 at 6:43














up vote
47
down vote

favorite
7












If I have really long output from a command (single line) but I know I only want the first [x] (let's say 8) characters of the output, what's the easiest way to get that? There aren't any delimiters.










share|improve this question























  • related: stackoverflow.com/questions/1405611/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Aug 7 at 6:43












up vote
47
down vote

favorite
7









up vote
47
down vote

favorite
7






7





If I have really long output from a command (single line) but I know I only want the first [x] (let's say 8) characters of the output, what's the easiest way to get that? There aren't any delimiters.










share|improve this question















If I have really long output from a command (single line) but I know I only want the first [x] (let's say 8) characters of the output, what's the easiest way to get that? There aren't any delimiters.







command-line shell text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 24 '10 at 3:24

























asked Oct 23 '10 at 23:07









xenoterracide

25.2k52157221




25.2k52157221











  • related: stackoverflow.com/questions/1405611/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Aug 7 at 6:43
















  • related: stackoverflow.com/questions/1405611/…
    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Aug 7 at 6:43















related: stackoverflow.com/questions/1405611/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Aug 7 at 6:43




related: stackoverflow.com/questions/1405611/…
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Aug 7 at 6:43










6 Answers
6






active

oldest

votes

















up vote
64
down vote



accepted










One way is to use cut:



 command | cut -c1-8


This will give you the first 8 characters of each line of output. Since cut is part of POSIX, it is likely to be on most Unices.






share|improve this answer
















  • 2




    Note that cut -c selects characters; cut -b or head -c selects bytes. This makes a difference in some locales (in practice, when using UTF-8).
    – Gilles
    Oct 24 '10 at 22:07










  • You also don't have to specify the start index in this case. Saying cut -c-8 will select from character 1 to 8.
    – Sparhawk
    May 9 '14 at 5:08










  • @Steven, cut's equivalent on Windows is?
    – Pacerier
    Aug 25 '15 at 13:06










  • Also command | dd bs=8 count=1 2>/dev/null. Not saying it's shorter or superior. Just another alternative.
    – dubiousjim
    Sep 24 '15 at 3:50










  • @Gilles, but note that with current versions of GNU cut, cut -c works like cut -b (that is, it doesn't work properly for multi-byte characters).
    – Stéphane Chazelas
    Aug 9 '16 at 13:49

















up vote
22
down vote













These are some other ways to get only first 8 characters.



command | head -c8

command | awk 'print substr($0,1,8);exit'

command | sed 's/^(........).*/1/;q'


And if you have bash



var=$(command)
echo $var:0:8





share|improve this answer
















  • 1




    I think the following sed formulation is a bit easier to read: command | sed 's/(.8).*/1/' or if your sed supports it: command | sed -r 's/(.8).*/1/'; Otherwise, +1
    – Steven D
    Oct 24 '10 at 4:48










  • Good stuff, but note that head -c counts bytes, not characters. Similarly, among the major Awk implementations, only GNU awk handles multi-byte characters correctly - FreeBSD Awk and Mawk do not.
    – mklement0
    Jul 5 '15 at 17:30

















up vote
1
down vote













If you have a sufficiently advanced shell (for example, the following will work in Bash, not sure about dash), you can do:



read -n8 -d$'' -r <(command)


After executing read ... <(command), your characters will be in the shell variable REPLY. Type help read to learn about other options.



Explanation: the -n8 argument to read says that we want up to 8 characters. The -d$'' says read until a null, rather than to a newline. This way the read will continue for 8 characters even if one of the earlier characters is a newline (but not if its a null). An alternative to -n8 -d$'' is to use -N8, which reads for exactly 8 characters or until the stdin reaches EOF. No delimiter is honored. That probably fits your needs better, but I don't know offhand how many shells have a read that honors -N as opposed to honoring -n and -d. Continuing with the explanation: -r says ignore -escapes, so that, for example, we treat \ as two characters, rather than as a single .



Finally, we do read ... <(command) rather than command | read ... because in the second form, the read is executed in a subshell which is then immediately exited, losing the information you just read.



Another option is to do all your processing inside the subshell. For example:



$ echo abcdefghijklm | read -n8 -d$'' -r; printf "REPLY=<%s>n" "$REPLY"; 
REPLY=<abcdefgh>





share|improve this answer
















  • 1




    If you just want to output the 8 chars, and don't need to process them in the shell, then just use cut.
    – dubiousjim
    Sep 8 '12 at 14:04










  • Good to know about read -n <num>; small caveat: Bash 3.x (still current on OS) mistakenly interprets <num> as a byte count and thus fails with multi-byte characters; this has been fixed in Bash 4.x.
    – mklement0
    Jul 6 '15 at 1:41

















up vote
1
down vote













This is portable:



a="$(command)" # Get the output of the command.
b="????" # as many ? as characters are needed.
echo $a%"$a#$b" # select that many chars from $a


To build a string of variable length of characters has its own question here.






share|improve this answer





























    up vote
    0
    down vote













    I had this problem when manually generating checksum files in maven repository.
    Unfortunately cut -c always prints out a newline at the end of output.
    To suppress that I use xxd:



    command | xxd -l$BYTES | xxd -r


    It outputs exactly $BYTES bytes, unless the command's output is shorter, then exactly that output.






    share|improve this answer



























      up vote
      0
      down vote













      Another one liner solution by using parameter expansion




      echo $word:0:x




      EG: word="Hello world"
      echo $word:0:3 or echo $word::3
      o/p: Hel


      EG.2: word="Hello world"
      echo $word:1:3
      o/p: ell





      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',
        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%2f3454%2fgrabbing-the-first-x-characters-for-a-string-from-a-pipe%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        64
        down vote



        accepted










        One way is to use cut:



         command | cut -c1-8


        This will give you the first 8 characters of each line of output. Since cut is part of POSIX, it is likely to be on most Unices.






        share|improve this answer
















        • 2




          Note that cut -c selects characters; cut -b or head -c selects bytes. This makes a difference in some locales (in practice, when using UTF-8).
          – Gilles
          Oct 24 '10 at 22:07










        • You also don't have to specify the start index in this case. Saying cut -c-8 will select from character 1 to 8.
          – Sparhawk
          May 9 '14 at 5:08










        • @Steven, cut's equivalent on Windows is?
          – Pacerier
          Aug 25 '15 at 13:06










        • Also command | dd bs=8 count=1 2>/dev/null. Not saying it's shorter or superior. Just another alternative.
          – dubiousjim
          Sep 24 '15 at 3:50










        • @Gilles, but note that with current versions of GNU cut, cut -c works like cut -b (that is, it doesn't work properly for multi-byte characters).
          – Stéphane Chazelas
          Aug 9 '16 at 13:49














        up vote
        64
        down vote



        accepted










        One way is to use cut:



         command | cut -c1-8


        This will give you the first 8 characters of each line of output. Since cut is part of POSIX, it is likely to be on most Unices.






        share|improve this answer
















        • 2




          Note that cut -c selects characters; cut -b or head -c selects bytes. This makes a difference in some locales (in practice, when using UTF-8).
          – Gilles
          Oct 24 '10 at 22:07










        • You also don't have to specify the start index in this case. Saying cut -c-8 will select from character 1 to 8.
          – Sparhawk
          May 9 '14 at 5:08










        • @Steven, cut's equivalent on Windows is?
          – Pacerier
          Aug 25 '15 at 13:06










        • Also command | dd bs=8 count=1 2>/dev/null. Not saying it's shorter or superior. Just another alternative.
          – dubiousjim
          Sep 24 '15 at 3:50










        • @Gilles, but note that with current versions of GNU cut, cut -c works like cut -b (that is, it doesn't work properly for multi-byte characters).
          – Stéphane Chazelas
          Aug 9 '16 at 13:49












        up vote
        64
        down vote



        accepted







        up vote
        64
        down vote



        accepted






        One way is to use cut:



         command | cut -c1-8


        This will give you the first 8 characters of each line of output. Since cut is part of POSIX, it is likely to be on most Unices.






        share|improve this answer












        One way is to use cut:



         command | cut -c1-8


        This will give you the first 8 characters of each line of output. Since cut is part of POSIX, it is likely to be on most Unices.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 23 '10 at 23:20









        Steven D

        31.6k696108




        31.6k696108







        • 2




          Note that cut -c selects characters; cut -b or head -c selects bytes. This makes a difference in some locales (in practice, when using UTF-8).
          – Gilles
          Oct 24 '10 at 22:07










        • You also don't have to specify the start index in this case. Saying cut -c-8 will select from character 1 to 8.
          – Sparhawk
          May 9 '14 at 5:08










        • @Steven, cut's equivalent on Windows is?
          – Pacerier
          Aug 25 '15 at 13:06










        • Also command | dd bs=8 count=1 2>/dev/null. Not saying it's shorter or superior. Just another alternative.
          – dubiousjim
          Sep 24 '15 at 3:50










        • @Gilles, but note that with current versions of GNU cut, cut -c works like cut -b (that is, it doesn't work properly for multi-byte characters).
          – Stéphane Chazelas
          Aug 9 '16 at 13:49












        • 2




          Note that cut -c selects characters; cut -b or head -c selects bytes. This makes a difference in some locales (in practice, when using UTF-8).
          – Gilles
          Oct 24 '10 at 22:07










        • You also don't have to specify the start index in this case. Saying cut -c-8 will select from character 1 to 8.
          – Sparhawk
          May 9 '14 at 5:08










        • @Steven, cut's equivalent on Windows is?
          – Pacerier
          Aug 25 '15 at 13:06










        • Also command | dd bs=8 count=1 2>/dev/null. Not saying it's shorter or superior. Just another alternative.
          – dubiousjim
          Sep 24 '15 at 3:50










        • @Gilles, but note that with current versions of GNU cut, cut -c works like cut -b (that is, it doesn't work properly for multi-byte characters).
          – Stéphane Chazelas
          Aug 9 '16 at 13:49







        2




        2




        Note that cut -c selects characters; cut -b or head -c selects bytes. This makes a difference in some locales (in practice, when using UTF-8).
        – Gilles
        Oct 24 '10 at 22:07




        Note that cut -c selects characters; cut -b or head -c selects bytes. This makes a difference in some locales (in practice, when using UTF-8).
        – Gilles
        Oct 24 '10 at 22:07












        You also don't have to specify the start index in this case. Saying cut -c-8 will select from character 1 to 8.
        – Sparhawk
        May 9 '14 at 5:08




        You also don't have to specify the start index in this case. Saying cut -c-8 will select from character 1 to 8.
        – Sparhawk
        May 9 '14 at 5:08












        @Steven, cut's equivalent on Windows is?
        – Pacerier
        Aug 25 '15 at 13:06




        @Steven, cut's equivalent on Windows is?
        – Pacerier
        Aug 25 '15 at 13:06












        Also command | dd bs=8 count=1 2>/dev/null. Not saying it's shorter or superior. Just another alternative.
        – dubiousjim
        Sep 24 '15 at 3:50




        Also command | dd bs=8 count=1 2>/dev/null. Not saying it's shorter or superior. Just another alternative.
        – dubiousjim
        Sep 24 '15 at 3:50












        @Gilles, but note that with current versions of GNU cut, cut -c works like cut -b (that is, it doesn't work properly for multi-byte characters).
        – Stéphane Chazelas
        Aug 9 '16 at 13:49




        @Gilles, but note that with current versions of GNU cut, cut -c works like cut -b (that is, it doesn't work properly for multi-byte characters).
        – Stéphane Chazelas
        Aug 9 '16 at 13:49












        up vote
        22
        down vote













        These are some other ways to get only first 8 characters.



        command | head -c8

        command | awk 'print substr($0,1,8);exit'

        command | sed 's/^(........).*/1/;q'


        And if you have bash



        var=$(command)
        echo $var:0:8





        share|improve this answer
















        • 1




          I think the following sed formulation is a bit easier to read: command | sed 's/(.8).*/1/' or if your sed supports it: command | sed -r 's/(.8).*/1/'; Otherwise, +1
          – Steven D
          Oct 24 '10 at 4:48










        • Good stuff, but note that head -c counts bytes, not characters. Similarly, among the major Awk implementations, only GNU awk handles multi-byte characters correctly - FreeBSD Awk and Mawk do not.
          – mklement0
          Jul 5 '15 at 17:30














        up vote
        22
        down vote













        These are some other ways to get only first 8 characters.



        command | head -c8

        command | awk 'print substr($0,1,8);exit'

        command | sed 's/^(........).*/1/;q'


        And if you have bash



        var=$(command)
        echo $var:0:8





        share|improve this answer
















        • 1




          I think the following sed formulation is a bit easier to read: command | sed 's/(.8).*/1/' or if your sed supports it: command | sed -r 's/(.8).*/1/'; Otherwise, +1
          – Steven D
          Oct 24 '10 at 4:48










        • Good stuff, but note that head -c counts bytes, not characters. Similarly, among the major Awk implementations, only GNU awk handles multi-byte characters correctly - FreeBSD Awk and Mawk do not.
          – mklement0
          Jul 5 '15 at 17:30












        up vote
        22
        down vote










        up vote
        22
        down vote









        These are some other ways to get only first 8 characters.



        command | head -c8

        command | awk 'print substr($0,1,8);exit'

        command | sed 's/^(........).*/1/;q'


        And if you have bash



        var=$(command)
        echo $var:0:8





        share|improve this answer












        These are some other ways to get only first 8 characters.



        command | head -c8

        command | awk 'print substr($0,1,8);exit'

        command | sed 's/^(........).*/1/;q'


        And if you have bash



        var=$(command)
        echo $var:0:8






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 24 '10 at 4:34









        user1606

        62933




        62933







        • 1




          I think the following sed formulation is a bit easier to read: command | sed 's/(.8).*/1/' or if your sed supports it: command | sed -r 's/(.8).*/1/'; Otherwise, +1
          – Steven D
          Oct 24 '10 at 4:48










        • Good stuff, but note that head -c counts bytes, not characters. Similarly, among the major Awk implementations, only GNU awk handles multi-byte characters correctly - FreeBSD Awk and Mawk do not.
          – mklement0
          Jul 5 '15 at 17:30












        • 1




          I think the following sed formulation is a bit easier to read: command | sed 's/(.8).*/1/' or if your sed supports it: command | sed -r 's/(.8).*/1/'; Otherwise, +1
          – Steven D
          Oct 24 '10 at 4:48










        • Good stuff, but note that head -c counts bytes, not characters. Similarly, among the major Awk implementations, only GNU awk handles multi-byte characters correctly - FreeBSD Awk and Mawk do not.
          – mklement0
          Jul 5 '15 at 17:30







        1




        1




        I think the following sed formulation is a bit easier to read: command | sed 's/(.8).*/1/' or if your sed supports it: command | sed -r 's/(.8).*/1/'; Otherwise, +1
        – Steven D
        Oct 24 '10 at 4:48




        I think the following sed formulation is a bit easier to read: command | sed 's/(.8).*/1/' or if your sed supports it: command | sed -r 's/(.8).*/1/'; Otherwise, +1
        – Steven D
        Oct 24 '10 at 4:48












        Good stuff, but note that head -c counts bytes, not characters. Similarly, among the major Awk implementations, only GNU awk handles multi-byte characters correctly - FreeBSD Awk and Mawk do not.
        – mklement0
        Jul 5 '15 at 17:30




        Good stuff, but note that head -c counts bytes, not characters. Similarly, among the major Awk implementations, only GNU awk handles multi-byte characters correctly - FreeBSD Awk and Mawk do not.
        – mklement0
        Jul 5 '15 at 17:30










        up vote
        1
        down vote













        If you have a sufficiently advanced shell (for example, the following will work in Bash, not sure about dash), you can do:



        read -n8 -d$'' -r <(command)


        After executing read ... <(command), your characters will be in the shell variable REPLY. Type help read to learn about other options.



        Explanation: the -n8 argument to read says that we want up to 8 characters. The -d$'' says read until a null, rather than to a newline. This way the read will continue for 8 characters even if one of the earlier characters is a newline (but not if its a null). An alternative to -n8 -d$'' is to use -N8, which reads for exactly 8 characters or until the stdin reaches EOF. No delimiter is honored. That probably fits your needs better, but I don't know offhand how many shells have a read that honors -N as opposed to honoring -n and -d. Continuing with the explanation: -r says ignore -escapes, so that, for example, we treat \ as two characters, rather than as a single .



        Finally, we do read ... <(command) rather than command | read ... because in the second form, the read is executed in a subshell which is then immediately exited, losing the information you just read.



        Another option is to do all your processing inside the subshell. For example:



        $ echo abcdefghijklm | read -n8 -d$'' -r; printf "REPLY=<%s>n" "$REPLY"; 
        REPLY=<abcdefgh>





        share|improve this answer
















        • 1




          If you just want to output the 8 chars, and don't need to process them in the shell, then just use cut.
          – dubiousjim
          Sep 8 '12 at 14:04










        • Good to know about read -n <num>; small caveat: Bash 3.x (still current on OS) mistakenly interprets <num> as a byte count and thus fails with multi-byte characters; this has been fixed in Bash 4.x.
          – mklement0
          Jul 6 '15 at 1:41














        up vote
        1
        down vote













        If you have a sufficiently advanced shell (for example, the following will work in Bash, not sure about dash), you can do:



        read -n8 -d$'' -r <(command)


        After executing read ... <(command), your characters will be in the shell variable REPLY. Type help read to learn about other options.



        Explanation: the -n8 argument to read says that we want up to 8 characters. The -d$'' says read until a null, rather than to a newline. This way the read will continue for 8 characters even if one of the earlier characters is a newline (but not if its a null). An alternative to -n8 -d$'' is to use -N8, which reads for exactly 8 characters or until the stdin reaches EOF. No delimiter is honored. That probably fits your needs better, but I don't know offhand how many shells have a read that honors -N as opposed to honoring -n and -d. Continuing with the explanation: -r says ignore -escapes, so that, for example, we treat \ as two characters, rather than as a single .



        Finally, we do read ... <(command) rather than command | read ... because in the second form, the read is executed in a subshell which is then immediately exited, losing the information you just read.



        Another option is to do all your processing inside the subshell. For example:



        $ echo abcdefghijklm | read -n8 -d$'' -r; printf "REPLY=<%s>n" "$REPLY"; 
        REPLY=<abcdefgh>





        share|improve this answer
















        • 1




          If you just want to output the 8 chars, and don't need to process them in the shell, then just use cut.
          – dubiousjim
          Sep 8 '12 at 14:04










        • Good to know about read -n <num>; small caveat: Bash 3.x (still current on OS) mistakenly interprets <num> as a byte count and thus fails with multi-byte characters; this has been fixed in Bash 4.x.
          – mklement0
          Jul 6 '15 at 1:41












        up vote
        1
        down vote










        up vote
        1
        down vote









        If you have a sufficiently advanced shell (for example, the following will work in Bash, not sure about dash), you can do:



        read -n8 -d$'' -r <(command)


        After executing read ... <(command), your characters will be in the shell variable REPLY. Type help read to learn about other options.



        Explanation: the -n8 argument to read says that we want up to 8 characters. The -d$'' says read until a null, rather than to a newline. This way the read will continue for 8 characters even if one of the earlier characters is a newline (but not if its a null). An alternative to -n8 -d$'' is to use -N8, which reads for exactly 8 characters or until the stdin reaches EOF. No delimiter is honored. That probably fits your needs better, but I don't know offhand how many shells have a read that honors -N as opposed to honoring -n and -d. Continuing with the explanation: -r says ignore -escapes, so that, for example, we treat \ as two characters, rather than as a single .



        Finally, we do read ... <(command) rather than command | read ... because in the second form, the read is executed in a subshell which is then immediately exited, losing the information you just read.



        Another option is to do all your processing inside the subshell. For example:



        $ echo abcdefghijklm | read -n8 -d$'' -r; printf "REPLY=<%s>n" "$REPLY"; 
        REPLY=<abcdefgh>





        share|improve this answer












        If you have a sufficiently advanced shell (for example, the following will work in Bash, not sure about dash), you can do:



        read -n8 -d$'' -r <(command)


        After executing read ... <(command), your characters will be in the shell variable REPLY. Type help read to learn about other options.



        Explanation: the -n8 argument to read says that we want up to 8 characters. The -d$'' says read until a null, rather than to a newline. This way the read will continue for 8 characters even if one of the earlier characters is a newline (but not if its a null). An alternative to -n8 -d$'' is to use -N8, which reads for exactly 8 characters or until the stdin reaches EOF. No delimiter is honored. That probably fits your needs better, but I don't know offhand how many shells have a read that honors -N as opposed to honoring -n and -d. Continuing with the explanation: -r says ignore -escapes, so that, for example, we treat \ as two characters, rather than as a single .



        Finally, we do read ... <(command) rather than command | read ... because in the second form, the read is executed in a subshell which is then immediately exited, losing the information you just read.



        Another option is to do all your processing inside the subshell. For example:



        $ echo abcdefghijklm | read -n8 -d$'' -r; printf "REPLY=<%s>n" "$REPLY"; 
        REPLY=<abcdefgh>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 8 '12 at 14:02









        dubiousjim

        1,9581223




        1,9581223







        • 1




          If you just want to output the 8 chars, and don't need to process them in the shell, then just use cut.
          – dubiousjim
          Sep 8 '12 at 14:04










        • Good to know about read -n <num>; small caveat: Bash 3.x (still current on OS) mistakenly interprets <num> as a byte count and thus fails with multi-byte characters; this has been fixed in Bash 4.x.
          – mklement0
          Jul 6 '15 at 1:41












        • 1




          If you just want to output the 8 chars, and don't need to process them in the shell, then just use cut.
          – dubiousjim
          Sep 8 '12 at 14:04










        • Good to know about read -n <num>; small caveat: Bash 3.x (still current on OS) mistakenly interprets <num> as a byte count and thus fails with multi-byte characters; this has been fixed in Bash 4.x.
          – mklement0
          Jul 6 '15 at 1:41







        1




        1




        If you just want to output the 8 chars, and don't need to process them in the shell, then just use cut.
        – dubiousjim
        Sep 8 '12 at 14:04




        If you just want to output the 8 chars, and don't need to process them in the shell, then just use cut.
        – dubiousjim
        Sep 8 '12 at 14:04












        Good to know about read -n <num>; small caveat: Bash 3.x (still current on OS) mistakenly interprets <num> as a byte count and thus fails with multi-byte characters; this has been fixed in Bash 4.x.
        – mklement0
        Jul 6 '15 at 1:41




        Good to know about read -n <num>; small caveat: Bash 3.x (still current on OS) mistakenly interprets <num> as a byte count and thus fails with multi-byte characters; this has been fixed in Bash 4.x.
        – mklement0
        Jul 6 '15 at 1:41










        up vote
        1
        down vote













        This is portable:



        a="$(command)" # Get the output of the command.
        b="????" # as many ? as characters are needed.
        echo $a%"$a#$b" # select that many chars from $a


        To build a string of variable length of characters has its own question here.






        share|improve this answer


























          up vote
          1
          down vote













          This is portable:



          a="$(command)" # Get the output of the command.
          b="????" # as many ? as characters are needed.
          echo $a%"$a#$b" # select that many chars from $a


          To build a string of variable length of characters has its own question here.






          share|improve this answer
























            up vote
            1
            down vote










            up vote
            1
            down vote









            This is portable:



            a="$(command)" # Get the output of the command.
            b="????" # as many ? as characters are needed.
            echo $a%"$a#$b" # select that many chars from $a


            To build a string of variable length of characters has its own question here.






            share|improve this answer














            This is portable:



            a="$(command)" # Get the output of the command.
            b="????" # as many ? as characters are needed.
            echo $a%"$a#$b" # select that many chars from $a


            To build a string of variable length of characters has its own question here.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 12:39









            Community

            1




            1










            answered Aug 23 '15 at 7:12







            user79743



























                up vote
                0
                down vote













                I had this problem when manually generating checksum files in maven repository.
                Unfortunately cut -c always prints out a newline at the end of output.
                To suppress that I use xxd:



                command | xxd -l$BYTES | xxd -r


                It outputs exactly $BYTES bytes, unless the command's output is shorter, then exactly that output.






                share|improve this answer
























                  up vote
                  0
                  down vote













                  I had this problem when manually generating checksum files in maven repository.
                  Unfortunately cut -c always prints out a newline at the end of output.
                  To suppress that I use xxd:



                  command | xxd -l$BYTES | xxd -r


                  It outputs exactly $BYTES bytes, unless the command's output is shorter, then exactly that output.






                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    I had this problem when manually generating checksum files in maven repository.
                    Unfortunately cut -c always prints out a newline at the end of output.
                    To suppress that I use xxd:



                    command | xxd -l$BYTES | xxd -r


                    It outputs exactly $BYTES bytes, unless the command's output is shorter, then exactly that output.






                    share|improve this answer












                    I had this problem when manually generating checksum files in maven repository.
                    Unfortunately cut -c always prints out a newline at the end of output.
                    To suppress that I use xxd:



                    command | xxd -l$BYTES | xxd -r


                    It outputs exactly $BYTES bytes, unless the command's output is shorter, then exactly that output.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 5 '17 at 17:28









                    Krzysztof Jabłoński

                    1084




                    1084




















                        up vote
                        0
                        down vote













                        Another one liner solution by using parameter expansion




                        echo $word:0:x




                        EG: word="Hello world"
                        echo $word:0:3 or echo $word::3
                        o/p: Hel


                        EG.2: word="Hello world"
                        echo $word:1:3
                        o/p: ell





                        share|improve this answer
























                          up vote
                          0
                          down vote













                          Another one liner solution by using parameter expansion




                          echo $word:0:x




                          EG: word="Hello world"
                          echo $word:0:3 or echo $word::3
                          o/p: Hel


                          EG.2: word="Hello world"
                          echo $word:1:3
                          o/p: ell





                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Another one liner solution by using parameter expansion




                            echo $word:0:x




                            EG: word="Hello world"
                            echo $word:0:3 or echo $word::3
                            o/p: Hel


                            EG.2: word="Hello world"
                            echo $word:1:3
                            o/p: ell





                            share|improve this answer












                            Another one liner solution by using parameter expansion




                            echo $word:0:x




                            EG: word="Hello world"
                            echo $word:0:3 or echo $word::3
                            o/p: Hel


                            EG.2: word="Hello world"
                            echo $word:1:3
                            o/p: ell






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered yesterday









                            Prabhat Kumar Singh

                            1153




                            1153



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f3454%2fgrabbing-the-first-x-characters-for-a-string-from-a-pipe%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 check contact read email or not when send email to Individual?

                                Bahrain

                                Postfix configuration issue with fips on centos 7; mailgun relay