user2732292
user2732292

Reputation: 11

strange single quotes in shell script

I wrote such scripts in test.sh:

#!/bin/bash
CPPCHECK_PATH=haha
SRC_TMP_PATH=asdf
xmlPath=test
cmd="$CPPCHECK_PATH/cppcheck $SRC_TMP_PATH --xml 2>$xmlPath"
`$cmd` 

I run "sh -x test.sh" and the console output is:

sh -x test.sh 
+ CPPCHECK_PATH=haha
+ SRC_TMP_PATH=asdf
+ xmlPath=test
+ cmd='haha/cppcheck asdf --xml 2>test'
+ echo haha/cppcheck asdf --xml '2>test'
haha/cppcheck asdf --xml 2>test
++ haha/cppcheck asdf --xml '2>test'

why there is a pair of single quote around '2>test'?the script cannot run normally due to he single quotes. Anyone knows?

Upvotes: 1

Views: 184

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799310

The single quotes are there to tell you that it is being used as an argument rather than a redirect followed by a filename, since you can't include redirects in a variable this way. Remove the redirection from the variable and perform it separately.

Upvotes: 2

Related Questions