在 CentOS 上的 Jenkins 里,“换行符”通常涉及 三种场景,下面分别说明:
sh '''
echo "第一行"
echo "第二行"
'''
或
sh "echo 第一行 \n echo 第二行"
✅ Linux 默认换行符是 LF(\n)
推荐用 '''(三单引号):
steps {
sh '''
echo hello
echo world
'''
}
\r\n)\n)Manage Jenkins → Configure System → Git
Checkout over CIFS 不勾Convert CRLF to LF during checkout
.gitattributes仓库根目录添加:
* text=auto eol=lf
或针对脚本:
*.sh text eol=lf
writeFile file: 'test.txt', text: "line1\nline2\n"
cat > test.txt <<EOF
line1
line2
EOF
file file.txt
cat -A file.txt
yum install dos2unix
dos2unix file.txt
❌ Jenkins 在 Windows Agent 上跑 Shell
✅ 用 Linux Agent 或 sh 步骤
❌ 脚本在 Windows 编辑后上传
✅ 用 dos2unix
如果你是指 “Jenkins 控制台输出换行”、“邮件内容换行” 或 “参数换行”,可以告诉我具体场景,我可以给你更精确的写法。