Skip to main content
added 3 characters in body
Source Link
DavidPostill
  • 163.9k
  • 78
  • 379
  • 421

This is my script, test.sh

if ssh myhost true; then echo 1 fi echo 2
if ssh myhost true; then
  echo 1
fi
echo 2

p.s. I have setup ssh key for myhost, so it won't ask password.

If I run it via bash, it will output two lines

$ bash test.sh 1 2
$ bash test.sh
1
2

However, if I redirect the script to bash's stdin, it only output one line

$ bash < test.sh
1

Why do bash behave differently? Is this a bug of bash or a magic feature?

I have tried bash 4.3 and 4.4.

p.s. If replacing "ssh myhost true" by "true"I replace ssh myhost true with true, the script canwill always output two lines.

This is my script, test.sh

if ssh myhost true; then echo 1 fi echo 2

p.s. I have setup ssh key for myhost, so it won't ask password.

If I run it via bash, it will output two lines

$ bash test.sh 1 2

However, if I redirect the script to bash's stdin, it only output one line

$ bash < test.sh
1

Why do bash behave differently? Is this a bug of bash or a magic feature?

I have tried bash 4.3 and 4.4.

p.s. If replacing "ssh myhost true" by "true", the script can always output two lines.

This is my script, test.sh

if ssh myhost true; then
  echo 1
fi
echo 2

p.s. I have setup ssh key for myhost, so it won't ask password.

If I run it via bash, it will output two lines

$ bash test.sh
1
2

However, if I redirect the script to bash's stdin, it only output one line

$ bash < test.sh
1

Why do bash behave differently? Is this a bug of bash or a magic feature?

I have tried bash 4.3 and 4.4.

p.s. If I replace ssh myhost true with true, the script will always output two lines.

Source Link
kcwu
  • 217
  • 1
  • 6

why do bash behave differently for script from stdin?

This is my script, test.sh

if ssh myhost true; then echo 1 fi echo 2

p.s. I have setup ssh key for myhost, so it won't ask password.

If I run it via bash, it will output two lines

$ bash test.sh 1 2

However, if I redirect the script to bash's stdin, it only output one line

$ bash < test.sh
1

Why do bash behave differently? Is this a bug of bash or a magic feature?

I have tried bash 4.3 and 4.4.

p.s. If replacing "ssh myhost true" by "true", the script can always output two lines.