【发布时间】:2019-05-02 06:41:42
【问题描述】:
我正在尝试在 bash 脚本中创建一个变量,但第一个空格会导致问题。
变量看起来像这样:
VAR="
--option1=foo \
--option2=bar \
--option3='something here with spaces'"
我正在尝试运行“make install”,但它总是抱怨第一个空格(即在上例中的“here”一词之前)。
如何创建带空格的变量?我尝试使用双引号,各种尝试转义单引号,但我无法工作。
我做错了什么?
下面的实际代码:
NGINX_OPTIONS="
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-ld-opt='-ljemalloc -Wl,-z,relro -Wl,-rpath,/usr/local/lib -Wl,--as-needed -pie' \
--with-cc-opt='-m64 -march=native -DTCP_FASTOPEN=23 -g -O3 -Wno-error=strict-aliasing -fstack-protector-strong -flto -fuse-ld=gold --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wno-deprecated-declarations -gsplit-dwarf'"
NGINX_MODULES="
--with-compat \
--with-threads \
--with-file-aio \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_mp4_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_sub_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module"
./configure $NGINX_OPTIONS $NGINX_MODULES
make
make install
【问题讨论】:
-
用反斜杠转义空格。像这样的东西:
--option3='something\ here\ with\ spaces' -
不起作用。说 ./configure: error: invalid option "here\" (我将这里的单词替换为第一个空格后的第二个参数)
-
用反斜杠转义引号,然后:
--option3=\'something here with spaces\' -
显示使用这个变量的代码?
-
可以是
Makfile吗?尝试将其复制到 shellscript 中,然后将/configure $NGINX_OPTIONS $NGINX_MODULES替换为echo $NGINX_OPTIONS $NGINX_MODULES