1

Answering to the following question: Allowed characters in linux environment variable names @aiden-bell writes that the following patterns gives all allowed shell variable names in BASH : [a-zA-Z_]+[a-zA-Z0-9_]*

I found this to be true. In fact I can export value _="Just for fun". Unfortunately though, whenever I print it I get __bp_preexec_invoke_exec

I went through this thread and while it is instructive it doesn't actually answer my question. Irrespective of whatever the shell might do with the variable $_, can I use it for my own means? Also, whatever exactly is __bp_preexec_invoke_exec? Thanks and regards.

Debabrata Roy
  • 177
  • 2
  • 17

1 Answers1

5

You can assign to the special parameter _, but the shell will also update its value after each command. Typically, you only use it as a dummy variable where you don't care about the result, such as in something like

while read _ second _ ; do ...; done < input.txt

where you only care about the second column of each input line.

From the man page:

  _      At  shell  startup,  set to the absolute pathname used to invoke
          the shell or shell script being executed as passed in the  envi-
          ronment  or  argument  list.   Subsequently, expands to the last
          argument to the previous command, after expansion.  Also set  to
          the  full  pathname  used  to  invoke  each command executed and
          placed in the environment exported to that command.  When check-
          ing  mail,  this  parameter holds the name of the mail file cur-
          rently being checked.
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 2
    Bash doesn't circumvent the read-only attribute, so you can stop it from being overwritten with `declare -r _="value"` if you'd rather have an error message per executed command :P – that other guy Jul 05 '17 at 21:42
  • @thatotherguy This is going to by April fools joke on all my colleagues' .bash_profile :P – Debabrata Roy Jul 06 '17 at 10:34