[英]Bash compare two strings with backslash in string
這不是我的確切問題,但是為了簡單起見,我們繼續。
如果運行以下命令,則需要此命令返回文件匹配項,但是由於某種原因,如果字符串中包含反斜杠,它將始終失敗。
文件testmonitor.txt僅包含以下文本
你好,世界
#!/bin/bash
file=$(cat /tmp/testmonitor.txt)
if [[ $file == $file ]]; then
echo "files match"
else
echo "files not matching"
fi
未加引號的右側被視為圖案,且圖案 hello\\ world
等同於模式hello world
,它不會在文字文本匹配hello\\ world
。
引用右側的內容以確保執行精確的字符串比較,而不是模式匹配。
if [[ $file == "$file" ]];
通常在雙引號中設置變量可解決此類問題
if [[ "$file" == "$file" ]]; then
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.