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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:20:10  来源:igfitidea点击:

Escape dollar sign in string by shell script

bashshell

提问by biubiubiu

Suppose I have a script named dd.sh, and I run it like this

假设我有一个名为 dd.sh 的脚本,我像这样运行它

./dd.sh sample$name.mp4

So $1is the string sample$name.mp4.

$1字符串也是如此sample$name.mp4

echo '' // shows 

echo "" // shows sample.mp4

Then how to process $1that I can detect whether there is a dollar sign in parameter $1

那么如何处理$1我可以检测参数中是否有美元符号$1

I want to process the string to sample\$name.mp4or 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 foorather than foo$bar$bazas the name of the current branch. foo$bar$bazwas getting assigned to PS1and $barand $bazwere then expanded. Escaping the dollar signs before including the branch name in PS1prevents 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 echobut 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 $filenamehaving the value sample.mp4(since $nameis 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 $filenamebecause of the single quotes.

echo "$filename"现在将导致预期的sample$name.mp4. 显然,由于单引号,echo '$filename'仍将仅打印$filename