0

The $_ variable, which should expand with the arguments of the last executed command in Bash, contains _lp_runtime_before. No matter how many times I run a command, that would be the content it has. Why?

Inian
  • 80,270
  • 14
  • 142
  • 161
Nico Rodsevich
  • 2,393
  • 2
  • 22
  • 32
  • 3
    Show us the whole script how you are using it, we need a minimal reproducible example to re-create your problem – Inian Oct 24 '19 at 12:46
  • 1
    Note that `$_` is an interactive feature -- not guaranteed to be available in scripts -- and further, that many shell-builtin variables are defined to lose their special behavior should anything assign to them explicitly. – Charles Duffy Oct 24 '19 at 13:10
  • 1
    So this is more a [Unix & Linux SE](https://unix.stackexchange.com/) question. – Amessihel Oct 24 '19 at 13:51
  • `$_` is a [special parameter](https://www.gnu.org/software/bash/manual/bash.html#Special-Parameters) that behaves the same in both interactive and non-interactive shells. – chepner Oct 24 '19 at 15:38
  • I would like to do any simple thing like `$ mkdir foo; cd $_` – Nico Rodsevich Oct 24 '19 at 21:34
  • I recreated the question there. Link: https://unix.stackexchange.com/questions/548630/bashs-var-not-expanding – Nico Rodsevich Oct 24 '19 at 21:47
  • 1
    @NicoRodsevich and slightly modifed it. :) – Amessihel Oct 25 '19 at 08:37

1 Answers1

1

Actually, $_ expand to the last argument of the last command line, according to the man page of bash:

[$_] expands to the last argument to the previous command, after expansion.

If you want the whole arguments, use !:*:

$ ls -a -l -h test
[blah blah]
$ last_command="!:*" > /dev/null

$ echo $last_command
-a -l -h test

I added a redirection of stdout to null device to prevent bash to print the expansion.

Amessihel
  • 5,891
  • 3
  • 16
  • 40
  • History expansion is typically off-topic here: SO is only for questions about writing software, and history expansion is only enabled by default in interactive shells (as opposed to those executing scripts). – Charles Duffy Oct 24 '19 at 13:11
  • @CharlesDuffy, TIL about expansion enabling and executing scripts. – Amessihel Oct 24 '19 at 13:50