Ell Neal
Ell Neal

Reputation: 6064

How to set a prefix to bash verbose mode

I'm writing a script using verbose mode but I'd like to set a prefix to the command outputs (mainly to make the output look nicer).

For example, the script:

#!/bin/bash -v

pwd
hostname

This will give the output:

pwd
/home/username
hostname
myawesomehost

But I'd like the output (specifically with the $ sign):

$ pwd
/home/username
$ hostname
myawesomehost

Is there a way to set a prefix for the verbose output like this?

Upvotes: 9

Views: 1399

Answers (1)

anubhava
anubhava

Reputation: 785306

You can use PS4 with set -x (enable trace):

#!/bin/bash

# prompt for trace commands
PS4='$ '

# enable trace
set -x

# remaining script
pwd
hostname

This will produce output as:

$ pwd
/home/username
$ hostname
myawesomehost

Upvotes: 7

Related Questions