I've read that you can redirect I/O into and out of various constructs in Bash (such as "if" and "while"). When I was trying this out, I noticed that using a "|" at the end of an if construct prevented any variables that I overwrote inside the if construct from taking effect; but if I use ">" instead, then the variable modifications take effect.
#!/bin/bash
VAR_0='Unmodified'
if true
then
VAR_0='Changed'
fi | cat
echo "VAR_0: $VAR_0"
###########
VAR_1='Unmodified'
if true
then
VAR_1='Changed'
fi > tmpFile
rm tmpFile
echo "VAR_1: $VAR_1"
Running Bash version 4.3.11 on 64-bit Linux produces the following output:
VAR_0: Unmodified
VAR_1: Changed
Note the only difference is how I'm redirecting stdout from the if construct. Why is the "|" preventing VAR_0 from being changed?
Aucun commentaire:
Enregistrer un commentaire