Getting different results running same commands step by step at a terminal and within a script [duplicate]












0















This question already has an answer here:




  • New line in bash variables

    3 answers




pid=$(pgrep 'engrampa') #Get the PID of the engrampa processes .
killpid=$(echo $pid | head -1) #Get only the first line of the $pid variable and put into a new variable called $killpid.
kill $killpid


I want to maintain only the first line of the variable $pid.



Let's say I have 3 instances of the engrampa process open.



When I run the commands above step by step at terminal, I get exactly what I want: 2590



https://imgur.com/a/Gf01Icd



When I run those exaclty commands in a script I get this result: 2590 18425 18449



https://imgur.com/a/Zi7HKkG



Why is that happening?










share|improve this question















marked as duplicate by muru, Community Dec 18 at 1:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Which shell are you using in the terminal? perhaps it is not word-splitting the unquoted $pid, whereas the non-interactive bash shell is (see for example Why are embedded newlines in command expansion replaced with whitespaces?)
    – steeldriver
    Dec 17 at 23:43












  • There is an excellent article on how to do process management at Greg's Wiki.
    – l0b0
    Dec 18 at 0:12
















0















This question already has an answer here:




  • New line in bash variables

    3 answers




pid=$(pgrep 'engrampa') #Get the PID of the engrampa processes .
killpid=$(echo $pid | head -1) #Get only the first line of the $pid variable and put into a new variable called $killpid.
kill $killpid


I want to maintain only the first line of the variable $pid.



Let's say I have 3 instances of the engrampa process open.



When I run the commands above step by step at terminal, I get exactly what I want: 2590



https://imgur.com/a/Gf01Icd



When I run those exaclty commands in a script I get this result: 2590 18425 18449



https://imgur.com/a/Zi7HKkG



Why is that happening?










share|improve this question















marked as duplicate by muru, Community Dec 18 at 1:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Which shell are you using in the terminal? perhaps it is not word-splitting the unquoted $pid, whereas the non-interactive bash shell is (see for example Why are embedded newlines in command expansion replaced with whitespaces?)
    – steeldriver
    Dec 17 at 23:43












  • There is an excellent article on how to do process management at Greg's Wiki.
    – l0b0
    Dec 18 at 0:12














0












0








0








This question already has an answer here:




  • New line in bash variables

    3 answers




pid=$(pgrep 'engrampa') #Get the PID of the engrampa processes .
killpid=$(echo $pid | head -1) #Get only the first line of the $pid variable and put into a new variable called $killpid.
kill $killpid


I want to maintain only the first line of the variable $pid.



Let's say I have 3 instances of the engrampa process open.



When I run the commands above step by step at terminal, I get exactly what I want: 2590



https://imgur.com/a/Gf01Icd



When I run those exaclty commands in a script I get this result: 2590 18425 18449



https://imgur.com/a/Zi7HKkG



Why is that happening?










share|improve this question
















This question already has an answer here:




  • New line in bash variables

    3 answers




pid=$(pgrep 'engrampa') #Get the PID of the engrampa processes .
killpid=$(echo $pid | head -1) #Get only the first line of the $pid variable and put into a new variable called $killpid.
kill $killpid


I want to maintain only the first line of the variable $pid.



Let's say I have 3 instances of the engrampa process open.



When I run the commands above step by step at terminal, I get exactly what I want: 2590



https://imgur.com/a/Gf01Icd



When I run those exaclty commands in a script I get this result: 2590 18425 18449



https://imgur.com/a/Zi7HKkG



Why is that happening?





This question already has an answer here:




  • New line in bash variables

    3 answers








shell-script head pgrep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 18 at 0:26

























asked Dec 17 at 23:16









Lucas Rizzini

267




267




marked as duplicate by muru, Community Dec 18 at 1:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by muru, Community Dec 18 at 1:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    Which shell are you using in the terminal? perhaps it is not word-splitting the unquoted $pid, whereas the non-interactive bash shell is (see for example Why are embedded newlines in command expansion replaced with whitespaces?)
    – steeldriver
    Dec 17 at 23:43












  • There is an excellent article on how to do process management at Greg's Wiki.
    – l0b0
    Dec 18 at 0:12














  • 1




    Which shell are you using in the terminal? perhaps it is not word-splitting the unquoted $pid, whereas the non-interactive bash shell is (see for example Why are embedded newlines in command expansion replaced with whitespaces?)
    – steeldriver
    Dec 17 at 23:43












  • There is an excellent article on how to do process management at Greg's Wiki.
    – l0b0
    Dec 18 at 0:12








1




1




Which shell are you using in the terminal? perhaps it is not word-splitting the unquoted $pid, whereas the non-interactive bash shell is (see for example Why are embedded newlines in command expansion replaced with whitespaces?)
– steeldriver
Dec 17 at 23:43






Which shell are you using in the terminal? perhaps it is not word-splitting the unquoted $pid, whereas the non-interactive bash shell is (see for example Why are embedded newlines in command expansion replaced with whitespaces?)
– steeldriver
Dec 17 at 23:43














There is an excellent article on how to do process management at Greg's Wiki.
– l0b0
Dec 18 at 0:12




There is an excellent article on how to do process management at Greg's Wiki.
– l0b0
Dec 18 at 0:12










1 Answer
1






active

oldest

votes


















1














Edit: Identified my problem thanks to steeldriver comment.



Running echo $pid | head -1 in bash do nothing. Running the same command at zsh shell I get exactly what I want.



bash shell output -> 2590 18425 18449



zsh shell output -> 2590



That being stated, my problem has been solved changing the shell of the script to #!/bin/zsh.



Edit: Another solution, and more suitable, is just using echo $pid | awk '{print $1}' instead echo $pid | head -1. It works on both shell. Thanks to Christopher comment.






share|improve this answer























  • Your original version would have worked in bash if you'd quoted the variable: echo "$pid" | head -1. However you might want to consider using pgrep -n or pgrep -o (or pidof with the -s switch) so as to avoid piping to head altogether.
    – steeldriver
    Dec 18 at 1:16




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Edit: Identified my problem thanks to steeldriver comment.



Running echo $pid | head -1 in bash do nothing. Running the same command at zsh shell I get exactly what I want.



bash shell output -> 2590 18425 18449



zsh shell output -> 2590



That being stated, my problem has been solved changing the shell of the script to #!/bin/zsh.



Edit: Another solution, and more suitable, is just using echo $pid | awk '{print $1}' instead echo $pid | head -1. It works on both shell. Thanks to Christopher comment.






share|improve this answer























  • Your original version would have worked in bash if you'd quoted the variable: echo "$pid" | head -1. However you might want to consider using pgrep -n or pgrep -o (or pidof with the -s switch) so as to avoid piping to head altogether.
    – steeldriver
    Dec 18 at 1:16


















1














Edit: Identified my problem thanks to steeldriver comment.



Running echo $pid | head -1 in bash do nothing. Running the same command at zsh shell I get exactly what I want.



bash shell output -> 2590 18425 18449



zsh shell output -> 2590



That being stated, my problem has been solved changing the shell of the script to #!/bin/zsh.



Edit: Another solution, and more suitable, is just using echo $pid | awk '{print $1}' instead echo $pid | head -1. It works on both shell. Thanks to Christopher comment.






share|improve this answer























  • Your original version would have worked in bash if you'd quoted the variable: echo "$pid" | head -1. However you might want to consider using pgrep -n or pgrep -o (or pidof with the -s switch) so as to avoid piping to head altogether.
    – steeldriver
    Dec 18 at 1:16
















1












1








1






Edit: Identified my problem thanks to steeldriver comment.



Running echo $pid | head -1 in bash do nothing. Running the same command at zsh shell I get exactly what I want.



bash shell output -> 2590 18425 18449



zsh shell output -> 2590



That being stated, my problem has been solved changing the shell of the script to #!/bin/zsh.



Edit: Another solution, and more suitable, is just using echo $pid | awk '{print $1}' instead echo $pid | head -1. It works on both shell. Thanks to Christopher comment.






share|improve this answer














Edit: Identified my problem thanks to steeldriver comment.



Running echo $pid | head -1 in bash do nothing. Running the same command at zsh shell I get exactly what I want.



bash shell output -> 2590 18425 18449



zsh shell output -> 2590



That being stated, my problem has been solved changing the shell of the script to #!/bin/zsh.



Edit: Another solution, and more suitable, is just using echo $pid | awk '{print $1}' instead echo $pid | head -1. It works on both shell. Thanks to Christopher comment.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 18 at 0:41

























answered Dec 18 at 0:27









Lucas Rizzini

267




267












  • Your original version would have worked in bash if you'd quoted the variable: echo "$pid" | head -1. However you might want to consider using pgrep -n or pgrep -o (or pidof with the -s switch) so as to avoid piping to head altogether.
    – steeldriver
    Dec 18 at 1:16




















  • Your original version would have worked in bash if you'd quoted the variable: echo "$pid" | head -1. However you might want to consider using pgrep -n or pgrep -o (or pidof with the -s switch) so as to avoid piping to head altogether.
    – steeldriver
    Dec 18 at 1:16


















Your original version would have worked in bash if you'd quoted the variable: echo "$pid" | head -1. However you might want to consider using pgrep -n or pgrep -o (or pidof with the -s switch) so as to avoid piping to head altogether.
– steeldriver
Dec 18 at 1:16






Your original version would have worked in bash if you'd quoted the variable: echo "$pid" | head -1. However you might want to consider using pgrep -n or pgrep -o (or pidof with the -s switch) so as to avoid piping to head altogether.
– steeldriver
Dec 18 at 1:16





Popular posts from this blog

Morgemoulin

Scott Moir

Souastre