Skip to content

Instantly share code, notes, and snippets.

@jlinoff
Last active April 6, 2022 02:51
Show Gist options
  • Save jlinoff/19056b3fa152b7289e1ef07264467726 to your computer and use it in GitHub Desktop.
Save jlinoff/19056b3fa152b7289e1ef07264467726 to your computer and use it in GitHub Desktop.
Bash script that shows each executing statement with file, function and lineno information. It also executes set +x silently.
#!/bin/bash
#
# Show how to make statements verbose with custom information.
#
function fct() {
echo "$*"
}
##PS4='+ LINE:${BASH_SOURCE[0]}:${BASH_FUNCNAME[0]}:${LINENO}: '
##PS4='$(printf "+ \033[38;5;245m%-16s\033[0m " "${BASH_SOURCE[0]}:${LINENO}:")'
##PS4='$(printf "\033[38;5;247m+ %-20s\033[0m " "${BASH_SOURCE[0]}:${LINENO}:")'
PS4='$(printf "+ %s|%-20s " $(date +"%Y-%m-%dT%H:%M:%S") "${BASH_SOURCE[0]}|${FUNCNAME[0]}|${LINENO}|")'
set -x
# This will be verbose.
echo "#1"
echo "#2"
fct "fct: #1"
# This will be silent (not visible).
{ set +x; } 2>/dev/null
# This will not be verbose.
echo "#3"
echo "#4"
fct "fct: #2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment