BASH脚本变量中的空格麻烦



我正在尝试在bash脚本中创建一个变量,而第一个空间正在引起问题。

变量看起来像这样:

VAR="
   --option1=foo 
   --option2=bar 
   --option3='something here with spaces'"

我正在尝试运行一个" make install",它总是抱怨第一个空间(即,在上面的示例中的单词"此处"之前(。

如何使用空格创建变量?我尝试使用双引号,各种尝试逃脱单价的尝试,但我无法工作。

我在做什么错?

实际代码下面:

            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

您尚未显示您如何使用此变量,但是如果我正确理解,您的命令在引号之外包含$VAR

问题是:

echo $VAR

等效于此:

echo "--option1=foo" "--option2=bar" "--option3='something" "here" "with" "spaces'"

因为变量内部的空格很重要,但是变量内的引号不是。

相反,您需要使用 array 。您可以声明这样的变量:

VAR=(
   --option1=foo
   --option2=bar
   --option3='something here with spaces'
)

并这样使用:

echo "${VAR[@]}"       # note the quotation-marks

等效于此:

echo "--option1=foo" "--option2=bar" "--option3=something here with spaces"

我尝试了逃脱引号的所有可能组合,最终以" configure"语句最终而没有双引用或单引号的变量:

    ./configure --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-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" 
        --with-ld-opt="-ljemalloc -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie" 
        $NGINX_MODULES

我也有一些错误,例如必须使用:

必须安装" jemalloc"
apt-get install libjemalloc-dev

这是从源构建NGINX的练习,而不是BASH脚本中的空间或引号。我希望这对我遇到的许多挑战有所帮助。

最新更新