interstar interstar - 1 year ago 63
Bash Question

How do I print a line of a shell-script in a shell-script?

I have a shell script that executes a command with a bunch of arguments that are, themselves, constructed by the script.

Eg. the script contains a line like this

do $opts x y z $more_opts a b c "bit in quotes" $stuff

I'd like to print out what this line actually is.

If I try :

echo 'do $opts x y z $more_opts a b c "bit in quotes" $stuff'

then I get the line with $opts and $more_opts being quoted literally rather than expanded into their values.

If I try backticks :

echo `do $opts x y z $more_opts a b c "bit in quotes" $stuff`

then it executes the line itself.

How can I generate and print a version of this string, with the $opts expanded into their values, but unexecuted?

Answer Source

The most accurate is:

echo do $opts x y z $more_opts a b c "bit in quotes" $stuff

since it will do the same word-splitting, filename-globbing, etc., as your actual command. (For example, if $stuff is *, this version will correctly list all the files in your directory, since that's what your real command will do.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download