gpt4 book ai didi

使用换行符时,Bash 会去除 "N"字符

转载 作者:行者123 更新时间:2023-12-04 18:40:17 25 4
gpt4 key购买 nike

我正在使用 Windows Linux 子系统(Ubuntu)。当我尝试设置换行符时,我丢失了“n”字符。我的简化脚本;

#!/bin/sh
echo $HOME #gives /home/hennio
IFS=$'\n'
echo $HOME #gives /home/he io
IFS=$'\n\b'没有解决问题。我用 $(which sh) 检查了我的 shebang,它是正确的(尽管使用了 zsh)。

在互联网上搜索没有给出任何结果。有人可以告诉我发生了什么吗?它让我发疯..

最佳答案

为了保持兼容性,shell 调用为 /bin/sh通常会尝试模拟 POSIX shell 或 Bourne shell 的某些变体。 POSIX 和 Bourne 都不支持 $'...' .有两种可能的解决方案:

方法一:使用shell的shebang,如bash , 支持 $'...' .

或者,

方法 2:使用 POSIX 方法为 IFS 分配换行符:

IFS='
'

(帽子提示: Gordon Davisson)

文档

来自 man zsh :

Zsh tries to emulate sh or ksh when it is invoked as sh or ksh respectively



来自 man bash :

If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.

关于使用换行符时,Bash 会去除 "N"字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49701041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com