{
  "$type": "site.standard.document",
  "canonicalUrl": "https://rednafi.com/misc/colon-command-in-shell-scripts/",
  "description": "Use the colon : command as a no-op in Bash scripts for cleaner debug output with -x flag. Alternative to echo for section markers and comments.",
  "path": "/misc/colon-command-in-shell-scripts/",
  "publishedAt": "2022-12-23T00:00:00.000Z",
  "site": "at://did:plc:fgtm2c26vfcj74rfmeggbyqj/site.standard.publication/3mnl6f7ob462z",
  "tags": [
    "Shell",
    "Unix",
    "TIL",
    "CLI"
  ],
  "textContent": "The colon : command is a shell utility that represents a truthy value. It can be thought\nof as an alias for the built-in true command. You can test it by opening a shell script\nand typing a colon on the command line, like this:\n\nIf you then inspect the exit code by typing $? on the command line, you'll see a 0\nthere, which is exactly what you'd see if you had used the true command.\n\nThe output will be:\n\nI find the colon command useful when running a shell script with the -x flag, which prints\nout the commands being executed by the interpreter. For example, consider the following\nscript:\n\nRunning this script with bash -x script.sh will print the following lines:\n\nNotice that the above script prints out each command first (denoted by a preceding + sign)\nand then its respective output. However, the echo \"section...\" commands in this script are\nonly used for debugging purposes, to enhance the readability of the output by providing\nseparation between different sections. Therefore, repeating these commands and their outputs\ncan be a little redundant. You can use the colon command to eliminate this repetition, as\nfollows:\n\nRunning this script with the -x flag will produce the following output:\n\nIf you look closely, you'll see that the debug commands and their outputs are no longer\ngetting repeated.\n\nFurther reading\n\n- [Why I use the colon command by @anthonywritescode]\n\n\n\n\n[why i use the colon command by @anthonywritescode]:\n    https://www.youtube.com/watch?v=onkNf1AKSgg",
  "title": "Colon command in shell scripts"
}