重击逃逸特殊字符

时间:2018-12-17 16:57:41

标签: bash shell

我正在同步目录。使用以下内容。

source_location='/pi/data/2018/image_*.png'
echo $source_location;

打印/pi/data/2018/image_*.png

mydate=$(date +'%Y');
source_location='/pi/data/$mydate/image_*.png'

打印/pi/data/2018/image_1.png /pi/data/2018/image_2.png /pi/data/2018/image_3.png等。

如何逃避*还是有更好的方法?

2 个答案:

答案 0 :(得分:0)

如果要存储全局扩展中的一系列文件名,请使用数组:

mydate=$(date +'%Y');
source_location=( "/pi/data/$mydate/image_"*.png )
echo "The first file is ${source_location[0]}"
echo "All files are: " "${source_location[@]}"

答案 1 :(得分:-2)

单引号不允许插值。双引号可以。

$: touch /pi/data/2018/image_foo.png           # create a file
$: touch /pi/data/2018/image_bar.png           # create a file
$: source_location='/pi/data/2018/image_*.png' # a single-quoted literal
$: echo '$source_location'                     # a single-quoted literal
$source_location
$: echo "$source_location"                     # a double-quoted variable
/pi/data/2018/image_*.png
$: echo $source_location                       # unquoted variable, parsed before passed to echo
/pi/data/2018/image_foo.png /pi/data/2018/image_bar.png

$: mydate=$(date +'%Y');
$: source_location='/pi/data/$mydate/image_*.png' # a single-quoted literal
$: echo '$source_location'                        # a single-quoted literal
$source_location
$: echo "$source_location"                        # a double-quoted variable containing a single-quoted literal
/pi/data/$mydate/image_*.png
$: source_location="/pi/data/$mydate/image_*.png" # a double-quoted variable
$: echo "$source_location"                        # a double-quoted variable containing a double-quoted variable
/pi/data/2018/image_*.png
$: echo $source_location                       # unquoted variable, parsed before passed to echo
/pi/data/2018/image_foo.png /pi/data/2018/image_bar.png

也许您应该发布一个更完整的问题,关于您要尝试完成的工作。另外,请先搜索该网站,然后再进行搜索-那里可能有很多与之相关的问题。