转义“$!”在bash中

问题描述 投票:0回答:1

我正在尝试转义特殊字符,看起来我已经捕获了大多数/几乎所有内容,除了 $! 的特定组合。

如果文件名同时包含 $!相邻的,则此脚本不再捕获这些字符,而是将其删除。

我正在努力完全理解为什么以及如何最好地调整这个脚本以保留“$!”

#run the executable with the params.
eval_args=()
for arg in "${@}"; do
    # Parsers previously had single quotes ("'/path/to/rom'" ), this allows those shortcuts to continue working. 
    removeLegacySingleQuotes=$(echo "$arg" | sed "s/^'//; s/'$//")
    escaped_arg=$(printf "%q" "$removeLegacySingleQuotes")
    eval_args+=("$escaped_arg")
done

echo "eval ${exe} "${eval_args[*]}""
# If the ROM has an "!" (ex: WarioWare: Twisted!), use set +H to temporarily allow !'s
set +H
eval "${exe}" "${eval_args[@]}"
set -H

示例:

./mgba.sh "WarioWare, Inc. - Mega Microgame$!.gba"
eval /home/deck/Applications/mGBA.AppImage WarioWare\,\ Inc.\ -\ Mega\ Microgame.gba

第二行表明它没有正确保存

$!

bash
1个回答
0
投票

您可以使用单引号

'
。将字符串包含在其中可以使其不受扩展和命令替换的影响。

单引号:

export TestVar=test
echo '$TestVar'
# Output is $TestVar

双引号:

export TestVar=test
echo "$TestVar"
# Output is test
© www.soinside.com 2019 - 2024. All rights reserved.