How to set prefix to bash verbose mode

I am writing a script using verbose mode, but I would like to set the output command prefix (mainly to make the output more enjoyable).

For example, a script:

#!/bin/bash -v

pwd
hostname

This will produce the result:

pwd
/home/username
hostname
myawesomehost

But I need a conclusion (especially with a sign $):

$ pwd
/home/username
$ hostname
myawesomehost

Is there a way to set the prefix for verbose output as follows?

+4
source share
1 answer

You can use PS4with set -x(enable tracing):

#!/bin/bash

# prompt for trace commands
PS4='$ '

# enable trace
set -x

# remaining script
pwd
hostname

This will output as:

$ pwd
/home/username
$ hostname
myawesomehost
+4
source

Source: https://habr.com/ru/post/1652688/


All Articles