Jimm Chen
Jimm Chen

Reputation: 3797

Have bash echo the final form of executed command, can I?

I have a small bash script build1c.sh .

if [ "$1" = "" ]; then
    echo "You must give a .c file to compile."
    exit 1
fi

cfile=$1
stem=${cfile%.*}

set -o verbose

gcc -c -g -Wall $cfile
gcc -o $stem $stem.o common.o reentrant.o -lssl -lcrypto

set +o verbose # optional here

The intention is to only echo the gcc commands being executed. I work to some extend. When I call build1c.sh client2.c , I see output

gcc -c -g -Wall $cfile
gcc -o $stem $stem.o common.o reentrant.o -lssl -lcrypto

set +o verbose # optional here

Still wacky, right? Those var reference($cfile, $stem) do not get their final form, so the echoing becomes pretty useless.

You know, what I like to see is

gcc -c -g -Wall client2.c
gcc -o client2 client2.o common.o reentrant.o -lssl -lcrypto

Is there correct and concise way to address this?

BTW: Minor request: Can I suppress the echoing of set +o verbose itself?

Upvotes: 1

Views: 205

Answers (2)

glenn jackman
glenn jackman

Reputation: 247210

function echo_and_execute {
    echo "$@"
    "$@"
}

echo_and_execute gcc -c -g -Wall $cfile
echo_and_execute gcc -o $stem $stem.o common.o reentrant.o -lssl -lcrypto

Upvotes: 2

sashang
sashang

Reputation: 12244

Replace set -o verbose with set -x

Upvotes: 2

Related Questions