Sagebrush Gardener
Sagebrush Gardener

Reputation: 416

set -x and wildcard expansion

In shell scripts, our corporate coding standard requires using...

set -x
command
set +x

...for logging, rather than...

echo "doing command"
command

However, when a wildcard is part of the command, this can produce very verbose output.

For example...

for i in {1..10}; do touch $i.foo; done; # create 10 foo files
set -x   # log command execution (stdout to be redirected to log file)
rm *.foo # delete foo files
set +x   # end logging

...produces the output...

rm 10.foo 1.foo 2.foo 3.foo 4.foo 5.foo 6.foo 7.foo 8.foo 9.foo

Okay for 10 files, but not so great for 10,000.

The desired output is...

rm *.foo

My first thought was to put *.foo in quotes...

rm "*.foo"

However, that gives the error...

rm: cannot remove ‘*.foo’: No such file or directory

Is there a way, using set -x, to echo the command without expanding the wildcard?

Upvotes: 0

Views: 80

Answers (1)

dash-o
dash-o

Reputation: 14452

For many cases, where simple '-x' or '-v' do not work (as per comments above), and staying within your coding standard (no separate echo), consider:

VAR=/tmp/123
$SHELL -cv "ls $VAR/*"

which will execute the command, but will log the command WITH variable substitution, command substitution, but WITHOUT wild-card substitution.

Upvotes: 1

Related Questions