我正在尝试创建一个SQL查询,并试图将其分配给某个变量,但我无法做到这一点。
TABLENAME=foo
QUERY="select * from $TABLENAME"
echo $QUERY
我期望查询将是 select * from foo
取而代之的是 select [file names in the directory] from foo
这正是我要得到的,而不是得到原始查询。
从foo中选择Anaconda3-5.3.1-Linux-x86_64.sh hadoop-2.7.3 hadoop-2.7.3.tar.gz script.sh
您必须引用变量:
echo "$QUERY"
否则,shell会将扩展*
为当前目录中的文件。
尝试echo "*"
并echo *
在您的外壳中。
这*
是您的Shell功能(可能是Bash)的一部分,称为Pathname Expansion。
从bash手册页:
特殊模式字符具有以下含义:
* Matches any string, including the null string. When the globstar shell option is enabled, and * is used in a pathname expansion context, two adjacent *s used as a single pattern will match all files and zero or more directories and subdi- rectories. If followed by a /, two adjacent *s will match only directories and subdirectories.
为防止这种情况,您可以*
使用反斜杠转义\
,也可以使用双引号"
或单引号将其引号'
例子:
# no pathname expansion
$ echo \*
*
$ echo "*"
*
$ echo '*'
*
# pathname expansion
$ echo *
file_x file_y file_z_in_this_directory
单引号引起的问题是,它将阻止变量的扩展(也称为参数扩展)。所有字符均按字面意义进行解释。
# no variable expansion
$ echo '$QUERY'
$QUERY
# variable expansion
$ echo "$QUERY"
select * from foo
# variable expansion and pathname expansion
$ echo $QUERY
select file_x file_y file_z_in_this_directory from foo
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句