mainThe GNU Bourne-Again SHell - Support: sr #109840, bash does not execute traps...

 
 

sr #109840: bash does not execute traps defined in eval

Submitter:  None
Submitted:  Thu 11 Jul 2019 09:00:30 AM UTC
   
 
Category:  None Priority:  5 - Normal
Severity:  3 - Normal Status:  Done
Privacy:  Public Assigned to:  None
Originator Email:  -email is unavailable- Open/Closed:  Open
Operating System:  GNU/Linux
* Mandatory Fields

Add a New Comment Rich Markup
   

Thu 05 Jun 2025 02:16:19 PM UTC, comment #3: 


> I don't know why they use this pipe here,


To close `STDIN`; they should have used `</dev/null` instead; see
https://gitlab.com/gitlab-org/gitlab-runner/-/issues/27668

This is larger than "traps in eval": actually the return code is not propagated correctly


$ bash -e -c ':|eval "sh -e -c \"exit 12\""'; echo $?
1
$ bash --posix -e -c ':|eval "sh -e -c \"exit 12\""'; echo $?
1
$ busybox sh -e -c ':|eval "sh -e -c \"exit 12\""'; echo $?
12
$ dash -e -c ':|eval "sh -e -c \"exit 12\""'; echo $?
12
$ bash -e -c 'eval "exec sh -e -c \"exit 12\""'; echo $?
12
$ bash -e -c 'eval "sh -e -c \"exit 12\" || exit \$?"'; echo $?
12
$ bash -e -c 'eval "sh -e -c \"exit 12\""'; echo $?
12
$ bash -e -c ':|sh -e -c "exit 12"'; echo $?
12
$ bash -e -c ':|eval "exit 12"'; echo $?
12
$ bash -e -c ' : |(eval "sh -e -c \"exit 12\"")'; echo $?
12


Philipp Hahn <pmhahn>
Tue 13 Aug 2019 02:11:28 PM UTC, comment #2: 

I got it. It will be fixed in the next devel branch push.

Chet Ramey <chet>
Group administrator
Wed 17 Jul 2019 03:29:09 PM UTC, comment #1: 

So when is the trap supposed to execute? When the subshell that runs the right side of the pipe exits?

Chet Ramey <chet>
Group administrator
Thu 11 Jul 2019 09:00:30 AM UTC, original submission:  

This was discovered while building a CI script with gitlab, see related issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/41581

Gitlab executes the a script with an available shell in the following way:

> : | eval "<script>"


see https://gitlab.com/gitlab-org/gitlab-runner/blob/master/shells/bash.go#L205

I don't know why they use this pipe here, but when a trap is defined inside an eval does not fire the trap when the shell exits:

> $ bash -c ": | eval 'trap \"echo TRAP\" EXIT'"
> $


While it works fine in dash and zsh:

> $ dash -c ": | eval 'trap \"echo TRAP\" EXIT'"
> TRAP
> $


> $ zsh -c ": | eval 'trap \"echo TRAP\" EXIT'"
> TRAP
> $


And it works also in bash, when i run the eval in a subshell:

> $ bash -c ": | (eval 'trap \"echo TRAP\" EXIT')"
> TRAP
> $


Note: without the pipe, it is also working:

> $ bash -c "eval 'trap \"echo TRAP\" EXIT'"
> TRAP
> $


Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by pmhahn (Posted a comment)
  • -email is unavailable- added by chet (Posted a comment)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-08-13 chet StatusNeed Info Done
    2019-08-13 chet StatusNone Need Info

    Back to the top

    Powered by Savane 3.15-e6e5.
    Corresponding source code