反斜杠从用户输入中消失 (read -p)
Back-slashes are disappearing from user input (read -p)
在我的脚本中,我请求用户指定一个路径。因为我在 Windows 我想将所有 \
替换为 /
。这本来是一件容易的事,但我遇到了一些麻烦:
read -p "Please, type/paste the working path (folder) you wish to link this scripts: " working_dir
I already did what this answers said:
tr '\' '/'
https://superuser.com/a/1068082/634144
home_mf="${home//\//}"
sed 's/\/\//g'
But I have no luck. This is what I do:
working_dir=$(echo "$working_dir" | <some of the pipe I typed before>)
echo $working_dir
编辑:
所有引用的文字对问题都没有用。我以为问题出在这里。但是在 read -p
命令下回显 $working_dir:
read -p "Please, type/paste the working path (folder) you wish to link this scripts: " working_dir
echo $working_dir
输出这个:
为什么反斜杠消失了?我的逻辑认为 B
和 G
也应该转义,或者我错了?
您想使用 read
的 -r
开关 不允许反斜杠转义任何字符(如 help read
中所写) .
所以这会起作用:
read -rp "Please, type/paste the working path (folder) you wish to link this scripts: " working_dir
working_dir=${working_dir//\//}
在我的脚本中,我请求用户指定一个路径。因为我在 Windows 我想将所有 \
替换为 /
。这本来是一件容易的事,但我遇到了一些麻烦:
read -p "Please, type/paste the working path (folder) you wish to link this scripts: " working_dir
I already did what this answers said:
tr '\' '/'
https://superuser.com/a/1068082/634144
home_mf="${home//\//}"
sed 's/\/\//g'
But I have no luck. This is what I do:
working_dir=$(echo "$working_dir" | <some of the pipe I typed before>) echo $working_dir
编辑:
所有引用的文字对问题都没有用。我以为问题出在这里。但是在 read -p
命令下回显 $working_dir:
read -p "Please, type/paste the working path (folder) you wish to link this scripts: " working_dir
echo $working_dir
输出这个:
为什么反斜杠消失了?我的逻辑认为 B
和 G
也应该转义,或者我错了?
您想使用 read
的 -r
开关 不允许反斜杠转义任何字符(如 help read
中所写) .
所以这会起作用:
read -rp "Please, type/paste the working path (folder) you wish to link this scripts: " working_dir
working_dir=${working_dir//\//}