Differences between revisions 9 and 14 (spanning 5 versions)
Revision 9 as of 2007-03-20 00:34:41
Size: 1943
Editor: pD9E48914
Comment: possible answer to question about output capturing without starting subshell
Revision 14 as of 2023-06-23 18:42:54
Size: 2398
Editor: larryv
Comment: fixed sloppily added entry
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
If you want to help with the BashFaq, you could try to answer one of the following questions. Just answer it, copy it to the BashFaq page, and remove the question here. If you want to help with the BashFAQ, you could try to answer one of the following questions. Just answer it, copy it to a new subpage of the BashFAQ page (e.g. [[BashFAQ/101]]), and remove the question here.
Line 8: Line 8:
 * How can I ensure that only once instance of my script is running at a time?
  * Answer: e.g. lockfile. This should be answered in the UnixFaq
  * It's also BashFaq #45 now.
 * How can I handle command line arguments in a shell script?
  * {{{getopts}}}, or a while/shift loop (see BashFaq #35)
 * How can I have variable variables, e.g. myvar=prefix$othervar?
  * Answer: {{{eval}}}
  * If {{{othervar}}} is an integer, use an array instead.
Line 17: Line 9:
  * This can't be done in any straightforward manner because there's no hook in Bash to execute arbitrary code after a user presses Enter but before a command is executed. The DEBUG trap is triggered after each command, and the PS1 variable is triggered when a prompt is displayed, which is also after the command, rather than before.
  * In bash 4, the DEBUG trap is triggered ''before'' every command, according to `help trap`. This is a change from bash 3. So this should be possible in bash 4. Someone just needs to write it up....
Line 18: Line 12:
  * date -d'yesterday' # requires GNU date
 * How to convert Unix time stamps to a date (and vice versa)?
  * gdate -d "1970-01-01 UTC + $time seconds"
  * something involving awk and strftime()
  * perl -e "print scalar localtime $time, \"\n\""
 * How to redirect stderr to a pipeline? Now BashFaq #47
  * command 2>&1 | command
  * To discard stdout entirely: command 2>&1 >/dev/null | command
  * date -d'yesterday' # GNU date (sufficiently new versions)
  * date -d '1 day ago' # GNU date (all versions I have available to test)
  * date -r $(( $(date +%s) - 86400 )) # OpenBSD date
Line 29: Line 18:
  * i think this does the job, have not had time to test: http://tldp.org/LDP/abs/html/process-sub.html   * I think this does the job, have not had time to test: http://tldp.org/LDP/abs/html/process-sub.html (see also ProcessSubstitution on this site)
   * Commands in a process substitution ( >(cmd) or <(cmd) ) are definitely run in a subshell (or rather a full-blown background process). I'm not sure I understand what this question is really asking. Maybe it's related to [[BashFAQ/084|FAQ 84]].
 * Creating temporary files securely and ''portably'' ([[BashFAQ/062]] has a partial answer)
 * Which shebang — {{{#!/bin/bash}}} or {{{#!/usr/bin/env bash}}} — should be used or in which circumstance? Is one preferred over the other?

Bash Open Questions

If you want to help with the BashFAQ, you could try to answer one of the following questions. Just answer it, copy it to a new subpage of the BashFAQ page (e.g. BashFAQ/101), and remove the question here.

  • How can I redirect the output of the script to both standard output and a log file?
    • Duplicating fds is not sufficient, because it duplicates the file descriptor number, not the data.
    • But you can still use tee: exec > >(tee log)

  • Somewhere the content of variables in my script lose whitespace.
  • How can I make bash set the xterm title to the command it is currently executing?
    • This can't be done in any straightforward manner because there's no hook in Bash to execute arbitrary code after a user presses Enter but before a command is executed. The DEBUG trap is triggered after each command, and the PS1 variable is triggered when a prompt is displayed, which is also after the command, rather than before.
    • In bash 4, the DEBUG trap is triggered before every command, according to help trap. This is a change from bash 3. So this should be possible in bash 4. Someone just needs to write it up....

  • How to determine Yesterday's date?
    • date -d'yesterday' # GNU date (sufficiently new versions)
    • date -d '1 day ago' # GNU date (all versions I have available to test)
    • date -r $(( $(date +%s) - 86400 )) # OpenBSD date
  • How can I redirect file names using sequential numbers to avoid overwriting existing ones?
    • mv --backup=numbered # requires GNU mv
  • How can I set the output of a command to a variable without executing a subshell (i.e. $() ) or writing to a file and reading it back?
    • I think this does the job, have not had time to test: http://tldp.org/LDP/abs/html/process-sub.html (see also ProcessSubstitution on this site)

      • Commands in a process substitution ( >(cmd) or <(cmd) ) are definitely run in a subshell (or rather a full-blown background process). I'm not sure I understand what this question is really asking. Maybe it's related to FAQ 84.

  • Creating temporary files securely and portably (BashFAQ/062 has a partial answer)

  • Which shebang — #!/bin/bash or #!/usr/bin/env bash — should be used or in which circumstance? Is one preferred over the other?

BashOpenQuestions (last edited 2023-06-23 18:42:54 by larryv)