bash 通过 shell 脚本在字符串中转义美元符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37876778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Escape dollar sign in string by shell script
提问by biubiubiu
Suppose I have a script named dd.sh, and I run it like this
假设我有一个名为 dd.sh 的脚本,我像这样运行它
./dd.sh sample$name.mp4
So $1
is the string sample$name.mp4
.
$1
字符串也是如此sample$name.mp4
。
echo '' // shows
echo "" // shows sample.mp4
Then how to process $1
that I can detect whether there is a dollar sign in parameter $1
那么如何处理$1
我可以检测参数中是否有美元符号$1
I want to process the string to sample\$name.mp4
or just detect whether there is a dollar sign in parameter $filename
我想将字符串处理为sample\$name.mp4
或只是检测参数中是否有美元符号$filename
回答by pacholik
As you know, a dollar sign marks a variable. You have to take it into account when you are typing it.
如您所知,美元符号标记一个变量。你在打字时必须考虑到它。
You can escape the dollar
你可以逃避美元
./dd.sh "sample$name.mp4"
or just type it with single quotes
或者只用单引号输入
./dd.sh 'sample$name.mp4'
To check if there is a dollar sign in a variable, do
要检查变量中是否有美元符号,请执行
[[ $variable == *$* ]] && echo 'I HAZ A DOLAR!!!' || echo 'MEH'
回答by davidchambers
One option:
一种选择:
# Replace occurrences of $ with $ to prevent variable substitution:
filename="${filename//$/\$}"
I just realized my prompt was showing foo
rather than foo$bar$baz
as the name of the current branch. foo$bar$baz
was getting assigned to PS1
and $bar
and $baz
were then expanded. Escaping the dollar signs before including the branch name in PS1
prevents unwanted expansions.
我刚刚意识到我的提示是显示foo
而不是foo$bar$baz
当前分支的名称。foo$bar$baz
被分配到PS1
,$bar
然后$baz
被扩展。在包含分支名称之前转义美元符号PS1
可防止不必要的扩展。
回答by Ajeet Shah
If your question is:
如果您的问题是:
Then how to process $1 that I can detect whether there is a dollar sign in parameter $1
那么如何处理$1,我可以检测参数$1中是否有美元符号
You can try this:
你可以试试这个:
if [[ == *'$'* ]]
then
echo '$ was found'
else
echo '$ was not found'
fi
Output:
输出:
$ ./dd.sh 'sample$name.mp4' // prints $ was found
$ ./dd.sh 'samplename.mp4' // prints $ was not found
回答by Sergey Kozlovskiy
For example you have .envfile with variables and password for postgres DB. As you know password should be urlencoded course %sing in password. So we have a problem here. Because BASH ignore $and we get always wrong password for encode.
例如,您有.env文件,其中包含 postgres DB 的变量和密码。如您所知,密码应该是 urlencoded course %sing in password。所以我们这里有问题。因为 BASH 忽略$并且我们总是得到错误的编码密码。
.env file
.env 文件
DB_NAME=sone_db DB_PASS=A1$Bb%!Y$ # with dollar signs ...
bash script
bash 脚本
#!/bin/bash PSQL_COMMAND="DROP schema public CASCADE;" PSQL_COMMAND+="CREATE schema public;" set -o allexport # set source file and get access to all variables in .env source /path/.env ENCODED_PASS=$(python -c "from urllib.parse import quote; print(quote('$DB_PASS'))"); psql postgres://$DB_USER:$ENCODED_PASS@$DB_HOST:5432/$DB_NAME -c "$PSQL_COMMAND" echo $DB_PASS # returns A1%!Y$ echo '$DB_PASS' # returns $DB_PASS echo "$DB_PASS" # returns A1%!Y$ # disables variables set +o allexport # Wont work because BASH find $ sing in string and think that is variable, so in first and last echo missed part $Bb%
To resolve this you need in .envfile escape string in single quote
要解决此问题,您需要在.env文件中使用单引号转义字符串
... DB_PASS='A1$Bb%!Y$' ...
回答by Kusalananda
Your issue is not with the echo
but with the assignment to $filename
.
您的问题不在于 ,echo
而在于分配给$filename
。
You say
你说
filename="sample$name.mp4"
This will interpolate the string, which means expanding the variable $name
. This will result in $filename
having the value sample.mp4
(since $name
is presumably undefined, which means it expands to an empty string)
这将插入字符串,这意味着扩展变量$name
。这将导致$filename
具有值sample.mp4
(因为$name
大概是未定义的,这意味着它扩展为空字符串)
Instead, use single quotes in the assignment:
相反,在赋值中使用单引号:
filename='sample$name.mp4'
echo "$filename"
will now result in the expected sample$name.mp4
. Obviously, echo '$filename'
will still just print $filename
because of the single quotes.
echo "$filename"
现在将导致预期的sample$name.mp4
. 显然,由于单引号,echo '$filename'
仍将仅打印$filename
。