Skip to content

Instantly share code, notes, and snippets.

@mjambon
Created July 19, 2018 18:02
Show Gist options
  • Save mjambon/4b3c5287f7778b9302e47a9c4396f25c to your computer and use it in GitHub Desktop.
Save mjambon/4b3c5287f7778b9302e47a9c4396f25c to your computer and use it in GitHub Desktop.
Subshells. Is the output 00, 11, 01, or 10?
#! /bin/bash
x=0
y=0
read x <<EOF
1
EOF
echo 1 | read y
echo "$x$y"
@mjambon
Copy link
Author

mjambon commented Jul 19, 2018

The program prints 10.

@mjambon
Copy link
Author

mjambon commented Jul 19, 2018

The pipe construct creates a subshell, so y is set in the subshell only.

$ echo 123 | (read y; echo $y)
123

but

$ y=0; echo 1 | read y; echo $y
0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment