Cygwin,bash,sed和导出导致的奇怪行为

时间:2018-09-10 19:08:55

标签: bash sed cygwin

对不起。这个问题让我非常困扰,直到凌晨3点才上床睡觉。那时我的大脑很乱,旧的问题描述一团糟。我已经重写了问题描述,并删除了旧的问题描述。


我发现有关Cygwin,bash,sed和export的奇怪行为。 将以下代码保存到文件中,例如/r/a.sh

echo PATH is $PATH
echo the sed in use is `which sed`

echo 1 | sed 's|1|22|g'

result=`echo 1 | sed 's|1|22|g'`
echo \`xxx\` without export
echo the result is \"$result\"
echo \`xxx\` with export
export result=`echo 1 | sed 's|1|22|g'`
echo the result is \"$result\"
echo \$\(xxx\) without export
result="$(echo 1 | sed 's|1|22|g')"
echo the result is \"$result\"
echo \$\(xxx\) with export
export result="$(echo 1 | sed 's|1|22|g')"
echo the result is \"$result\"

read

这四个案例的预期结果均为22。对于我尝试过的大多数环境,例如Debian,zsh和在conmu中运行bash都是如此。在圆锥木中,结果是:

PATH is /usr/local/bin:/usr/bin:/d/PortableAppsLocal/ConEmu/ConEmu/Scripts:/d/PortableAppsLocal/ConEmu:/d/PortableAppsLocal/ConEmu/ConEmu:/c/Program Files (x86)/Intel/MPI-RT/4.1.3.045/em64t/bin:/c/Program Files (x86)/Intel/MPI-RT/4.1.3.045/ia32/bin:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/ProgramData/Oracle/Java/javapath:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/ProgramData/chocolatey/bin:/c/Program Files (x86)/GNU/GnuPG/pub:/c/Program Files/IBM/Bluemix/bin:/c/Program Files/MATLAB/R2018a/runtime/win64:/c/Program Files/MATLAB/R2018a/bin:/c/Program Files/Git/cmd:/c/Program Files (x86)/Subversion/bin:/c/WINDOWS/System32/OpenSSH:/c/Program Files/OpenVPN/bin:/c/Program Files (x86)/STMicroelectronics/STM32 ST-LINK Utility/ST-LINK Utility:/c/Users/user/AppData/Local/Microsoft/WindowsApps:/d/Cloud/Win+R:/usr/bin:/d/Python/Python36:/d/Python/Python36/Scripts:/c/Program Files/Oracle/VirtualBox:/d/PortableAppsLocal/bin:/d/Python/Python27:/d/Python/Python27/Scripts:/c/Program Files (x86)/Subversion/bin:/c/Program Files/LLVM/bin:/d/gccarm/bin:/c/Program Files/Java/jre1.8.0_172/bin:/d/lint:/c/Users/user/AppData/Local/atom/bin:/c/Program Files/Microsoft VS Code/bin
the sed in use is /usr/bin/sed
22
`xxx` without export
the result is "22"
`xxx` with export
the result is "22"
$(xxx) without export
the result is "22"
$(xxx) with export
the result is "22"

但是,如果我通过D:\ cygwin64 \ bin \ bash.exe --login /r/a.sh运行它,则结果为

PATH is /usr/local/bin:/usr/bin:/c/Program Files (x86)/Intel/MPI-RT/4.1.3.045/em64t/bin:/c/Program Files (x86)/Intel/MPI-RT/4.1.3.045/ia32/bin:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/ProgramData/Oracle/Java/javapath:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/ProgramData/chocolatey/bin:/c/Program Files (x86)/GNU/GnuPG/pub:/c/Program Files/IBM/Bluemix/bin:/c/Program Files/MATLAB/R2018a/runtime/win64:/c/Program Files/MATLAB/R2018a/bin:/c/Program Files/Git/cmd:/c/Program Files (x86)/Subversion/bin:/c/WINDOWS/System32/OpenSSH:/c/Program Files/OpenVPN/bin:/c/Program Files (x86)/STMicroelectronics/STM32 ST-LINK Utility/ST-LINK Utility:/c/Users/user/AppData/Local/Microsoft/WindowsApps:/d/Cloud/Win+R:/usr/bin:/d/Python/Python36:/d/Python/Python36/Scripts:/c/Program Files/Oracle/VirtualBox:/d/PortableAppsLocal/bin:/d/Python/Python27:/d/Python/Python27/Scripts:/c/Program Files (x86)/Subversion/bin:/c/Program Files/LLVM/bin:/d/gccarm/bin:/c/Program Files/Java/jre1.8.0_172/bin:/d/lint:/c/Users/user/AppData/Local/atom/bin:/c/Program Files/Microsoft VS Code/bin
the sed in use is /usr/bin/sed
22
`xxx` without export
the result is ""
`xxx` with export
the result is "22"
$(xxx) without export
the result is ""
$(xxx) with export
the result is ""

我也尝试过使用sed的绝对路径。

echo run with absolute path /sed/bin

echo 1 | sed 's|1|22|g'

result=`echo 1 | /bin/sed 's|1|22|g'`
echo \`xxx\` without export
echo the result is \"$result\"
echo \`xxx\` with export
export result=`echo 1 | /bin/sed 's|1|22|g'`
echo the result is \"$result\"
echo \$\(xxx\) without export
result="$(echo 1 | /bin/sed 's|1|22|g')"
echo the result is \"$result\"
echo \$\(xxx\) with export
export result="$(echo 1 | /bin/sed 's|1|22|g')"
echo the result is \"$result\"

read

结果仍然是

run with absolute path /sed/bin
22
`xxx` without export
the result is ""
`xxx` with export
the result is "22"
$(xxx) without export
the result is ""
$(xxx) with export
the result is ""

我还尝试了在不使用--login的情况下运行它,结果是相同的。

最后,有四种情况,并且只有export result = xxx可以正常工作。


我尝试在脚本开始处添加set -x,结果是

C:\Users\user>D:\cygwin64\bin\bash.exe --login /r/a.sh
+ echo PATH is /usr/local/bin:/usr/bin:/c/Program Files '(x86)/Intel/MPI-RT/4.1.3.045/em64t/bin:/c/Program' Files '(x86)/Intel/MPI-RT/4.1.3.045/ia32/bin:/c/Program' Files '(x86)/Common' Files/Oracle/Java/javapath:/c/ProgramData/Oracle/Java/javapath:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/ProgramData/chocolatey/bin:/c/Program Files '(x86)/GNU/GnuPG/pub:/c/Program' Files/IBM/Bluemix/bin:/c/Program Files/MATLAB/R2018a/runtime/win64:/c/Program Files/MATLAB/R2018a/bin:/c/Program Files/Git/cmd:/c/Program Files '(x86)/Subversion/bin:/c/WINDOWS/System32/OpenSSH:/c/Program' Files/OpenVPN/bin:/c/Program Files '(x86)/STMicroelectronics/STM32' ST-LINK Utility/ST-LINK Utility:/c/Users/user/AppData/Local/Microsoft/WindowsApps:/d/Cloud/Win+R:/usr/bin:/d/Python/Python36:/d/Python/Python36/Scripts:/c/Program Files/Oracle/VirtualBox:/d/PortableAppsLocal/bin:/d/Python/Python27:/d/Python/Python27/Scripts:/c/Program Files '(x86)/Subversion/bin:/c/Program' Files/LLVM/bin:/d/gccarm/bin:/c/Program Files/Java/jre1.8.0_172/bin:/d/lint:/c/Users/user/AppData/Local/atom/bin:/c/Program Files/Microsoft VS Code/bin
PATH is /usr/local/bin:/usr/bin:/c/Program Files (x86)/Intel/MPI-RT/4.1.3.045/em64t/bin:/c/Program Files (x86)/Intel/MPI-RT/4.1.3.045/ia32/bin:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/ProgramData/Oracle/Java/javapath:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/ProgramData/chocolatey/bin:/c/Program Files (x86)/GNU/GnuPG/pub:/c/Program Files/IBM/Bluemix/bin:/c/Program Files/MATLAB/R2018a/runtime/win64:/c/Program Files/MATLAB/R2018a/bin:/c/Program Files/Git/cmd:/c/Program Files (x86)/Subversion/bin:/c/WINDOWS/System32/OpenSSH:/c/Program Files/OpenVPN/bin:/c/Program Files (x86)/STMicroelectronics/STM32 ST-LINK Utility/ST-LINK Utility:/c/Users/user/AppData/Local/Microsoft/WindowsApps:/d/Cloud/Win+R:/usr/bin:/d/Python/Python36:/d/Python/Python36/Scripts:/c/Program Files/Oracle/VirtualBox:/d/PortableAppsLocal/bin:/d/Python/Python27:/d/Python/Python27/Scripts:/c/Program Files (x86)/Subversion/bin:/c/Program Files/LLVM/bin:/d/gccarm/bin:/c/Program Files/Java/jre1.8.0_172/bin:/d/lint:/c/Users/user/AppData/Local/atom/bin:/c/Program Files/Microsoft VS Code/bin

C:\Users\user>D:\cygwin64\bin\bash.exe --login /r/a.sh
+ echo PATH is /usr/local/bin:/usr/bin:/c/Program Files '(x86)/Intel/MPI-RT/4.1.3.045/em64t/bin:/c/Program' Files '(x86)/Intel/MPI-RT/4.1.3.045/ia32/bin:/c/Program' Files '(x86)/Common' Files/Oracle/Java/javapath:/c/ProgramData/Oracle/Java/javapath:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/ProgramData/chocolatey/bin:/c/Program Files '(x86)/GNU/GnuPG/pub:/c/Program' Files/IBM/Bluemix/bin:/c/Program Files/MATLAB/R2018a/runtime/win64:/c/Program Files/MATLAB/R2018a/bin:/c/Program Files/Git/cmd:/c/Program Files '(x86)/Subversion/bin:/c/WINDOWS/System32/OpenSSH:/c/Program' Files/OpenVPN/bin:/c/Program Files '(x86)/STMicroelectronics/STM32' ST-LINK Utility/ST-LINK Utility:/c/Users/user/AppData/Local/Microsoft/WindowsApps:/d/Cloud/Win+R:/usr/bin:/d/Python/Python36:/d/Python/Python36/Scripts:/c/Program Files/Oracle/VirtualBox:/d/PortableAppsLocal/bin:/d/Python/Python27:/d/Python/Python27/Scripts:/c/Program Files '(x86)/Subversion/bin:/c/Program' Files/LLVM/bin:/d/gccarm/bin:/c/Program Files/Java/jre1.8.0_172/bin:/d/lint:/c/Users/user/AppData/Local/atom/bin:/c/Program Files/Microsoft VS Code/bin
PATH is /usr/local/bin:/usr/bin:/c/Program Files (x86)/Intel/MPI-RT/4.1.3.045/em64t/bin:/c/Program Files (x86)/Intel/MPI-RT/4.1.3.045/ia32/bin:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/ProgramData/Oracle/Java/javapath:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/ProgramData/chocolatey/bin:/c/Program Files (x86)/GNU/GnuPG/pub:/c/Program Files/IBM/Bluemix/bin:/c/Program Files/MATLAB/R2018a/runtime/win64:/c/Program Files/MATLAB/R2018a/bin:/c/Program Files/Git/cmd:/c/Program Files (x86)/Subversion/bin:/c/WINDOWS/System32/OpenSSH:/c/Program Files/OpenVPN/bin:/c/Program Files (x86)/STMicroelectronics/STM32 ST-LINK Utility/ST-LINK Utility:/c/Users/user/AppData/Local/Microsoft/WindowsApps:/d/Cloud/Win+R:/usr/bin:/d/Python/Python36:/d/Python/Python36/Scripts:/c/Program Files/Oracle/VirtualBox:/d/PortableAppsLocal/bin:/d/Python/Python27:/d/Python/Python27/Scripts:/c/Program Files (x86)/Subversion/bin:/c/Program Files/LLVM/bin:/d/gccarm/bin:/c/Program Files/Java/jre1.8.0_172/bin:/d/lint:/c/Users/user/AppData/Local/atom/bin:/c/Program Files/Microsoft VS Code/bin
++ which sed
+ echo the sed in use is /usr/bin/sed
the sed in use is /usr/bin/sed
+ echo 1
+ sed 's|1|22|g'
22
+ result=
+ echo '`xxx`' without export
`xxx` without export
+ echo the result is '""'
the result is ""
+ echo '`xxx`' with export
`xxx` with export
++ echo 1
++ sed 's|1|22|g'
+ export result=22
+ result=22
+ echo the result is '"22"'
the result is "22"
+ echo '$(xxx)' without export
$(xxx) without export
+ result=
+ echo the result is '""'
the result is ""
+ echo '$(xxx)' with export
$(xxx) with export
+ export result=
+ result=
+ echo the result is '""'
the result is ""

0 个答案:

没有答案