Skip to main content
Formatting, alternative implementation of one example
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

As mentioned in other answers, the parts of a pipeline run in subshells, so modifications made there aren't visible to the main shell.

If we consider just Bash, there are two other workarounds in addition to the cmd | { stuff; more stuff; } structure:

  1. Redirect the input from process substitution:

    while read var ; do x=55 ; done < <(echo fred) echo "$x"

     while read var ; do x=55 ; done < <(echo fred)
     echo "$x"
    

The output from the command in <(...) is made to appear as if it were a named pipe.

  1. The lastpipe option, which makes Bash work like ksh, and runs the last part of the pipeline in the main shell process. Though it only works if job control is disabled, i.e. not in an interactive shell:

    bash -c ' shopt -s lastpipe echo fred | while read var ; do x=55 ; done; echo "$x" '

     bash -c '
       shopt -s lastpipe
       echo fred | while read var ; do x=55 ; done; 
       echo "$x"
     '
    

or

    bash -O lastpipe -c '
      echo fred | while read var ; do x=55 ; done; 
      echo "$x"
    '

Process substitution is of course supported in ksh and zsh too. But since they run the last part of the pipeline in the main shell anyway, using it as a workaround isn't really necessary.

As mentioned in other answers, the parts of a pipeline run in subshells, so modifications made there aren't visible to the main shell.

If we consider just Bash, there are two other workarounds in addition to the cmd | { stuff; more stuff; } structure:

  1. Redirect the input from process substitution:

    while read var ; do x=55 ; done < <(echo fred) echo "$x"

The output from the command in <(...) is made to appear as if it were a named pipe.

  1. The lastpipe option, which makes Bash work like ksh, and runs the last part of the pipeline in the main shell process. Though it only works if job control is disabled, i.e. not in an interactive shell:

    bash -c ' shopt -s lastpipe echo fred | while read var ; do x=55 ; done; echo "$x" '

Process substitution is of course supported in ksh and zsh too. But since they run the last part of the pipeline in the main shell anyway, using it as a workaround isn't really necessary.

As mentioned in other answers, the parts of a pipeline run in subshells, so modifications made there aren't visible to the main shell.

If we consider just Bash, there are two other workarounds in addition to the cmd | { stuff; more stuff; } structure:

  1. Redirect the input from process substitution:

     while read var ; do x=55 ; done < <(echo fred)
     echo "$x"
    

The output from the command in <(...) is made to appear as if it were a named pipe.

  1. The lastpipe option, which makes Bash work like ksh, and runs the last part of the pipeline in the main shell process. Though it only works if job control is disabled, i.e. not in an interactive shell:

     bash -c '
       shopt -s lastpipe
       echo fred | while read var ; do x=55 ; done; 
       echo "$x"
     '
    

or

    bash -O lastpipe -c '
      echo fred | while read var ; do x=55 ; done; 
      echo "$x"
    '

Process substitution is of course supported in ksh and zsh too. But since they run the last part of the pipeline in the main shell anyway, using it as a workaround isn't really necessary.

added 4 characters in body
Source Link
Stéphane Chazelas
  • 584.7k
  • 96
  • 1.1k
  • 1.7k

As mentioned in other answers, the parts of a pipeline run in subshells, so modifications made there aren't visible to the main shell.

If we consider just Bash, there are two other workarounds in addition to the cmd | { stuff; more stuff; } structure:

  1. Redirect the input from process substitution:

    while read var ; do x=55 ; done < <(echo fred) echo "$x"

The output from the command in <(...) is made to appear as if it were a named pipe.

  1. The lastpipe option, which makes Bash work like ksh, and runs the last part of the pipepipeline in the main shell process. Though it only works if job control is disabled, i.e. not in an interactive shell:

    bash -c ' shopt -s lastpipe echo fred | while read var ; do x=55 ; done; echo "$x" '

Process substitution is of course supported in ksh and zsh too. But since they run the last part of the pipeline in the main shell anyway, using it as a workaround isn't really necessary.

As mentioned in other answers, the parts of a pipeline run in subshells, so modifications made there aren't visible to the main shell.

If we consider just Bash, there are two other workarounds in addition to the cmd | { stuff; more stuff; } structure:

  1. Redirect the input from process substitution:

    while read var ; do x=55 ; done < <(echo fred) echo "$x"

The output from the command in <(...) is made to appear as if it were a named pipe.

  1. The lastpipe option, which makes Bash work like ksh, and runs the last part of the pipe in the main shell process. Though it only works if job control is disabled, i.e. not in an interactive shell:

    bash -c ' shopt -s lastpipe echo fred | while read var ; do x=55 ; done; echo "$x" '

Process substitution is of course supported in ksh and zsh too. But since they run the last part of the pipeline in the main shell anyway, using it as a workaround isn't really necessary.

As mentioned in other answers, the parts of a pipeline run in subshells, so modifications made there aren't visible to the main shell.

If we consider just Bash, there are two other workarounds in addition to the cmd | { stuff; more stuff; } structure:

  1. Redirect the input from process substitution:

    while read var ; do x=55 ; done < <(echo fred) echo "$x"

The output from the command in <(...) is made to appear as if it were a named pipe.

  1. The lastpipe option, which makes Bash work like ksh, and runs the last part of the pipeline in the main shell process. Though it only works if job control is disabled, i.e. not in an interactive shell:

    bash -c ' shopt -s lastpipe echo fred | while read var ; do x=55 ; done; echo "$x" '

Process substitution is of course supported in ksh and zsh too. But since they run the last part of the pipeline in the main shell anyway, using it as a workaround isn't really necessary.

Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

As mentioned in other answers, the parts of a pipeline run in subshells, so modifications made there aren't visible to the main shell.

If we consider just Bash, there are two other workarounds in addition to the cmd | { stuff; more stuff; } structure:

  1. Redirect the input from process substitution:

    while read var ; do x=55 ; done < <(echo fred) echo "$x"

The output from the command in <(...) is made to appear as if it were a named pipe.

  1. The lastpipe option, which makes Bash work like ksh, and runs the last part of the pipe in the main shell process. Though it only works if job control is disabled, i.e. not in an interactive shell:

    bash -c ' shopt -s lastpipe echo fred | while read var ; do x=55 ; done; echo "$x" '

Process substitution is of course supported in ksh and zsh too. But since they run the last part of the pipeline in the main shell anyway, using it as a workaround isn't really necessary.