Bash: Head & Tail behavior with bash script

后端 未结 3 1059
北荒
北荒 2020-12-29 04:49

Suppose I have following script:-

test.sh

#!/bin/bash
command1  #prints 5 lines
command2  #prints 3 lines

I run th

3条回答
  •  离开以前
    2020-12-29 05:44

    This is more of a comment then an answer but it is too big for a comment.

    I tried following script:

    #!/usr/bin/env bash
    
    rm -f "test_head.log"
    echo "1 line"
    echo "1 line" >> "test_head.log"
    echo "2 line"
    echo "2 line" >> "test_head.log"
    echo "3 line"
    echo "3 line" >> "test_head.log"
    echo "4 line"
    echo "4 line" >> "test_head.log"
    echo "5 line"
    echo "5 line" >> "test_head.log"
    echo "6 line"
    echo "6 line" >> "test_head.log"
    echo "7 line"
    echo "7 line" >> "test_head.log"
    echo "8 line"   
    echo "8 line" >> "test_head.log"
    

    Then i ran the script with:

    ./test_head.sh | head -n1

    The cat output is (to my surprise):

    1 line

    I have no idea what is going on.

    After reading @ymonad comment i tried it out and replace echo with /bin/echo and that solved the problem. I hope he can explain more about this behaviour.

提交回复
热议问题