Why does this shell script insert single quotes in a command?

I have the following shell script to create something:

#!/bin/bash -x

SRC_DIR=$1

cd $SRC_DIR

rm CMakeCache.txt
cmake \
-DCGAL_DIR=/src/path \
-DCMAKE_CXX_FLAGS=" -frounding-math -std=c++11 " \
-DTBB_INSTALL_DIR=/tbb/dir \
-DTBB_ARCH_PLATFORM=tbb_arch \
.

make VERBOSE=1

Now the -x option will show me the commands issued by the script. The call is cmakeas follows:

cmake -DCGAL_DIR = / src / path '-DCMAKE_CXX_FLAGS = -frounding-math -std = C ++ 11' -DTBB_INSTALL_DIR = / tbb / dir -DTBB_ARCH_PLATFORM = tbb_arch.

The call is processed without an error message, but watch single quotes 'around -DCMAKE_CXX_FLAGS=....

Where are they from? What is their effect?

+4
source share
3 answers

Single quotes appear in the trace output when bash finds an argument containing spaces.

Examples:

bash -cx 'echo foo "xyz 123"'
+ echo foo 'xyz 123'
foo xyz 123

bash -cx "echo foo 'xyz 123'"
+ echo foo 'xyz 123'
foo xyz 123
+1

Bash , . :

#!/bin/bash -x

echo Test "Test" " Test" "Test " ' Test '

:

+ echo Test Test ' Test' 'Test ' ' Test '
Test Test  Test Test   Test

-. cmake , .

+1

There is no difference between

-DCMAKE_CXX_FLAGS=" -frounding-math -std=c++11 "

and

'-DCMAKE_CXX_FLAGS= -frounding-math -std=c++11 '

These are two ways to write the same thing. Bash doesn't remember how you wrote it. Inside, it stores each token in an array with quotes removed. When he prints them, he only knows that it has spaces, so it’s better to quote it.

+1
source

Source: https://habr.com/ru/post/1685138/


All Articles