some sed commands fail when in background

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











up vote
0
down vote

favorite












Some sed commands seem to fail when run in background. Minimal example below.



One sed command appends append after a block of lines that begin with a star *. The other just substitutes a word. The order of the sed commands does not matter.



TEXT=$'* firstn'

two_seds()
sed "s/first/second/";


Executing two_seds & wait results to:



* second
append


While executing wait & two_seds yields:



* second


So the second sed command always works, while the first does nothing after an &.



While for now I know how to work around it, I would love to understand what's going on. Partly out of curiosity, partly so that it won't bite me later in obscure edge cases.



Can reproduce in bash or dash. Running:



Ubuntu 18.04
sed (GNU sed) 4.4
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
dash, version 0.5.8-2.10









share|improve this question

















  • 2




    Is it your intention to use $! as the last backgrounded-PID (since it's in double quotes), or as an address range "not the last line"?
    – Jeff Schaller
    Nov 27 at 18:08










  • The intention was to have it as "not the last line". The use of double quotes was because there are other variables there that need to be resolved. Apparently "not the last line" is not needed for my use case, so I can drop it.
    – Boffin
    Nov 28 at 8:58










  • Do you still see the problem after you drop the $! ?
    – JigglyNaga
    Nov 28 at 10:26










  • @JigglyNaga Nope, that solves it. It didn't do anything constructive anyways, since it's resolved to an empty string in the "working" case.
    – Boffin
    Nov 28 at 19:35














up vote
0
down vote

favorite












Some sed commands seem to fail when run in background. Minimal example below.



One sed command appends append after a block of lines that begin with a star *. The other just substitutes a word. The order of the sed commands does not matter.



TEXT=$'* firstn'

two_seds()
sed "s/first/second/";


Executing two_seds & wait results to:



* second
append


While executing wait & two_seds yields:



* second


So the second sed command always works, while the first does nothing after an &.



While for now I know how to work around it, I would love to understand what's going on. Partly out of curiosity, partly so that it won't bite me later in obscure edge cases.



Can reproduce in bash or dash. Running:



Ubuntu 18.04
sed (GNU sed) 4.4
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
dash, version 0.5.8-2.10









share|improve this question

















  • 2




    Is it your intention to use $! as the last backgrounded-PID (since it's in double quotes), or as an address range "not the last line"?
    – Jeff Schaller
    Nov 27 at 18:08










  • The intention was to have it as "not the last line". The use of double quotes was because there are other variables there that need to be resolved. Apparently "not the last line" is not needed for my use case, so I can drop it.
    – Boffin
    Nov 28 at 8:58










  • Do you still see the problem after you drop the $! ?
    – JigglyNaga
    Nov 28 at 10:26










  • @JigglyNaga Nope, that solves it. It didn't do anything constructive anyways, since it's resolved to an empty string in the "working" case.
    – Boffin
    Nov 28 at 19:35












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Some sed commands seem to fail when run in background. Minimal example below.



One sed command appends append after a block of lines that begin with a star *. The other just substitutes a word. The order of the sed commands does not matter.



TEXT=$'* firstn'

two_seds()
sed "s/first/second/";


Executing two_seds & wait results to:



* second
append


While executing wait & two_seds yields:



* second


So the second sed command always works, while the first does nothing after an &.



While for now I know how to work around it, I would love to understand what's going on. Partly out of curiosity, partly so that it won't bite me later in obscure edge cases.



Can reproduce in bash or dash. Running:



Ubuntu 18.04
sed (GNU sed) 4.4
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
dash, version 0.5.8-2.10









share|improve this question













Some sed commands seem to fail when run in background. Minimal example below.



One sed command appends append after a block of lines that begin with a star *. The other just substitutes a word. The order of the sed commands does not matter.



TEXT=$'* firstn'

two_seds()
sed "s/first/second/";


Executing two_seds & wait results to:



* second
append


While executing wait & two_seds yields:



* second


So the second sed command always works, while the first does nothing after an &.



While for now I know how to work around it, I would love to understand what's going on. Partly out of curiosity, partly so that it won't bite me later in obscure edge cases.



Can reproduce in bash or dash. Running:



Ubuntu 18.04
sed (GNU sed) 4.4
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
dash, version 0.5.8-2.10






bash shell-script sed regular-expression dash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 at 17:54









Boffin

1032




1032







  • 2




    Is it your intention to use $! as the last backgrounded-PID (since it's in double quotes), or as an address range "not the last line"?
    – Jeff Schaller
    Nov 27 at 18:08










  • The intention was to have it as "not the last line". The use of double quotes was because there are other variables there that need to be resolved. Apparently "not the last line" is not needed for my use case, so I can drop it.
    – Boffin
    Nov 28 at 8:58










  • Do you still see the problem after you drop the $! ?
    – JigglyNaga
    Nov 28 at 10:26










  • @JigglyNaga Nope, that solves it. It didn't do anything constructive anyways, since it's resolved to an empty string in the "working" case.
    – Boffin
    Nov 28 at 19:35












  • 2




    Is it your intention to use $! as the last backgrounded-PID (since it's in double quotes), or as an address range "not the last line"?
    – Jeff Schaller
    Nov 27 at 18:08










  • The intention was to have it as "not the last line". The use of double quotes was because there are other variables there that need to be resolved. Apparently "not the last line" is not needed for my use case, so I can drop it.
    – Boffin
    Nov 28 at 8:58










  • Do you still see the problem after you drop the $! ?
    – JigglyNaga
    Nov 28 at 10:26










  • @JigglyNaga Nope, that solves it. It didn't do anything constructive anyways, since it's resolved to an empty string in the "working" case.
    – Boffin
    Nov 28 at 19:35







2




2




Is it your intention to use $! as the last backgrounded-PID (since it's in double quotes), or as an address range "not the last line"?
– Jeff Schaller
Nov 27 at 18:08




Is it your intention to use $! as the last backgrounded-PID (since it's in double quotes), or as an address range "not the last line"?
– Jeff Schaller
Nov 27 at 18:08












The intention was to have it as "not the last line". The use of double quotes was because there are other variables there that need to be resolved. Apparently "not the last line" is not needed for my use case, so I can drop it.
– Boffin
Nov 28 at 8:58




The intention was to have it as "not the last line". The use of double quotes was because there are other variables there that need to be resolved. Apparently "not the last line" is not needed for my use case, so I can drop it.
– Boffin
Nov 28 at 8:58












Do you still see the problem after you drop the $! ?
– JigglyNaga
Nov 28 at 10:26




Do you still see the problem after you drop the $! ?
– JigglyNaga
Nov 28 at 10:26












@JigglyNaga Nope, that solves it. It didn't do anything constructive anyways, since it's resolved to an empty string in the "working" case.
– Boffin
Nov 28 at 19:35




@JigglyNaga Nope, that solves it. It didn't do anything constructive anyways, since it's resolved to an empty string in the "working" case.
– Boffin
Nov 28 at 19:35










1 Answer
1






active

oldest

votes

















up vote
10
down vote



accepted










The "$!" at the beginning of your sed script ("$!N;/.*n...) will expand to the PID of the last background job. Shell variables are expanded inside double quotes.



If no background job was yet run, it will expand to nothing, which is wrong, too (if you put the sed script in single quotes, it will go in an infinite loop).






share|improve this answer






















  • Thanks a lot for solving this mystery @pizdelect. Much to learn, I have.
    – Boffin
    Nov 28 at 9:00










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%2f484491%2fsome-sed-commands-fail-when-in-background%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
10
down vote



accepted










The "$!" at the beginning of your sed script ("$!N;/.*n...) will expand to the PID of the last background job. Shell variables are expanded inside double quotes.



If no background job was yet run, it will expand to nothing, which is wrong, too (if you put the sed script in single quotes, it will go in an infinite loop).






share|improve this answer






















  • Thanks a lot for solving this mystery @pizdelect. Much to learn, I have.
    – Boffin
    Nov 28 at 9:00














up vote
10
down vote



accepted










The "$!" at the beginning of your sed script ("$!N;/.*n...) will expand to the PID of the last background job. Shell variables are expanded inside double quotes.



If no background job was yet run, it will expand to nothing, which is wrong, too (if you put the sed script in single quotes, it will go in an infinite loop).






share|improve this answer






















  • Thanks a lot for solving this mystery @pizdelect. Much to learn, I have.
    – Boffin
    Nov 28 at 9:00












up vote
10
down vote



accepted







up vote
10
down vote



accepted






The "$!" at the beginning of your sed script ("$!N;/.*n...) will expand to the PID of the last background job. Shell variables are expanded inside double quotes.



If no background job was yet run, it will expand to nothing, which is wrong, too (if you put the sed script in single quotes, it will go in an infinite loop).






share|improve this answer














The "$!" at the beginning of your sed script ("$!N;/.*n...) will expand to the PID of the last background job. Shell variables are expanded inside double quotes.



If no background job was yet run, it will expand to nothing, which is wrong, too (if you put the sed script in single quotes, it will go in an infinite loop).







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 at 18:15

























answered Nov 27 at 18:07









pizdelect

16114




16114











  • Thanks a lot for solving this mystery @pizdelect. Much to learn, I have.
    – Boffin
    Nov 28 at 9:00
















  • Thanks a lot for solving this mystery @pizdelect. Much to learn, I have.
    – Boffin
    Nov 28 at 9:00















Thanks a lot for solving this mystery @pizdelect. Much to learn, I have.
– Boffin
Nov 28 at 9:00




Thanks a lot for solving this mystery @pizdelect. Much to learn, I have.
– Boffin
Nov 28 at 9:00

















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%2f484491%2fsome-sed-commands-fail-when-in-background%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