为什么在通过回声运行反斜杠消失? [英] Why do backslashes disappear when run through echo?

查看:125
本文介绍了为什么在通过回声运行反斜杠消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code这样的,它处理一个CSV文件:

I have code like this, which processes a CSV file:

#!/bin/bash
while read line
do
    variable=$(echo $line | awk -F, '{print $2}')
    echo $variable
 done < ./file.csv

如果CSV文件包含的任何 \\ ,当我运行此命令,输出文本不显示 \\

If the CSV file contains any \, when I run this command, the output text does not show the \.

我怎样才能确保 \\ 不会被删除?

How can I ensure that \ is not deleted?

推荐答案

这样做的原因行为是内建使用 \\ 作为转义字符。在 -r 标志禁用此行为。

The reason for this behaviour is that the read builtin uses \ as escape character. The -r flag disables this behaviour.

所以,这应该工作:

while read -r line
  variable=$(echo $line | awk -F, '{print $2}')
  echo $variable
done < ./file.csv

您还应该将...周围的事物,例如 $(...)和变量,像

You should also place "..." around things like $(...) and variables, like

variable="$(command)"
echo "$variable"

这篇关于为什么在通过回声运行反斜杠消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆