[英]Bash strips “N” character when using newline-delimiter
I am using Windows Linux Subsystem (Ubuntu). 我正在使用Windows Linux子系统(Ubuntu)。 When I try to set a newline-delimiter, I lose my 'n'-characters.
当我尝试设置换行符时,我丢失了'n'个字符。 My simplified script;
我的简化脚本;
#!/bin/sh
echo $HOME #gives /home/hennio
IFS=$'\n'
echo $HOME #gives /home/he io
IFS=$'\\n\\b'
didnt solve the problem. IFS=$'\\n\\b'
不能解决问题。 I checked my shebang with $(which sh), it is correct (although using zsh). 我用$(with sh)检查了我的shebang,它是正确的(尽管使用zsh)。
Searching on internet didnt give any results. 在互联网上搜索没有得到任何结果。 Can someone please tell me whats going on?
有人可以告诉我发生了什么吗? It driving me nuts..
它让我发疯..
To maintain compatibility, a shell invoked as /bin/sh
usually tries to emulate a POSIX shell or some variant of a Bourne shell. 为了保持兼容性,以
/bin/sh
调用的shell通常会尝试模仿POSIX shell或Bourne shell的某些变体。 Neither POSIX nor Bourne support $'...'
. POSIX和Bourne都不支持
$'...'
。 There are two possible solutions: 有两种可能的解决方案:
Method 1: Use the shebang of a shell, like bash
, that supports $'...'
. 方法1:使用支持
$'...'
的shell(例如bash
的shebang。
Or, 要么,
Method 2: Use a POSIX method to assigning a newline to IFS
: 方法2:使用POSIX方法向
IFS
分配换行符:
IFS='
'
(Hat tip: Gordon Davisson ) (帽子提示: 戈登·戴维森 )
From man zsh
: 来自
man zsh
:
Zsh
tries to emulatesh
orksh
when it is invoked assh
orksh
respectivelyZsh
分别作为sh
或ksh
调用时尝试模拟sh
或ksh
From man bash
: 从
man bash
:
If
bash
is invoked with the namesh
, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.如果使用名称
sh
调用bash
,则它会尝试尽可能接近于sh的历史版本的启动行为,同时也要符合POSIX标准。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.