Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_ (underscore) command wrongly updated if previous output was filtered #579

Open
caribpa opened this issue Feb 10, 2021 · 11 comments
Open
Labels
bug Something isn't working shell test-required

Comments

@caribpa
Copy link
Contributor

caribpa commented Feb 10, 2021

Work environment

Questions Answers
OS/arch/bits (mandatory) Arch Linux x64
File format of the file you reverse (mandatory) ELF
Architecture/bits of the file (mandatory) x86/64
rizin -v full output, not truncated (mandatory) rizin 0.2.0-git @ linux-x86-64 commit: 027b2c2, build: 2021-02-10__18:21:49

Expected behavior

_ is updated with the result of a previous grep (~) or a pipe (|).

Actual behavior

_ breaks and contains unknown/wrong output.

Steps to reproduce the behavior

$ rz -d /bin/rizin
> izq ~Usage
> echo `_~[0]`
> 💔

Whereas the following works as expected:

$ rz -d /bin/rizin
> izq
> echo `_~[0]`

Funnily, echo output changes (to something also wrong) if you enter visual panels mode (v) before izq ~Usage and then echo.

Note that _ only contains the output of the Rizin command if a pipe was used previously as a filter:

$ rz -d /bin/rizin
> izq | grep Usage
> echo `_~[0]`  # Result is like the previous command was just: izq
> 💔

This issue is also present in radare2.

@caribpa
Copy link
Contributor Author

caribpa commented Feb 10, 2021

By the way, opening a file in debug mode is not necessary for reproducing the issue.

@XVilka XVilka added the shell label Feb 10, 2021
@XVilka XVilka added bug Something isn't working test-required labels Feb 10, 2021
@XVilka XVilka added this to the 0.2.0 milestone Feb 10, 2021
@ret2libc
Copy link
Member

Ok, I can reproduce... However, I even wonder if this command is useful at all. Note that _ does not re-execute the command but is really just a low-level command that prints what was in the RzCons buffer with last command. Why would you want that? Shall we remove it? @XVilka @ITAYC0HEN @thestr4ng3r @wargio

Funnily, echo output changes (to something also wrong) if you enter visual panels mode (v) before izq ~Usage and then echo.

I believe this happens because visual messes up with the RzCons buffer

Note that _ only contains the output of the Rizin command if a pipe was used previously as a filter:

When you use | the final output is not copied to RzCons buffer but just printed out directly by the grep command. Again, _ is a low-level command IMO.

@ITAYC0HEN
Copy link
Member

I never used _ and don't know how it should be used. Maybe some commands are slow and heavy and you don't want to use run it over and over again if you test some things.

@XVilka
Copy link
Member

XVilka commented Feb 11, 2021

There is $(!!) in bash and $_ in zsh for this kind of thing. I never used it myself, but maybe useful for someone. Removal is fine for me too.

@caribpa
Copy link
Contributor Author

caribpa commented Feb 11, 2021

There is $(!!) in bash and $_ in zsh for this kind of thing. I never used it myself, but maybe useful for someone. Removal is fine for me too.

$_ is available in almost all shells, though it is not POSIX. It basically gets the last parameter passed to the previous command:

> echo hello world
# hello world
> echo $_ hello
# world hello
> echo $_ 
# hello

$(!!) works at least in bash and zsh, but it is not POSIX as well. !! re-executes the last command, and $() is wrapping its result (stdout) in a subshell. Maybe useful as an eval replacement:

> echo echo hello
# echo hello
> $(!!)
# hello

@caribpa
Copy link
Contributor Author

caribpa commented Feb 11, 2021

In Rizin I use _ for quickly filtering the last output (with ~) instead of calling the command again.

Imagine doing izz on a big file. In an ARM platform this operation takes some seconds.
After getting the output of ìzz, I can simply filter the output with _ ~Something[0] instead of repeating the command again.

_ is also useful when you forgot to save the output of a command (to a file or variable for running multiple filters) and you don't want to repeat it again because it may take time (decompiling with ghidra in a less powerful platform).

@ret2libc
Copy link
Member

Ok so some questions:

  • izz => you expect _ to contain the output of izz
  • izz~string => you expect _ to contain the output of izz~string, right?
  • izz;_~string;_~string2; => you expect _ to contain the output of izz and keep its value even after executing _~string?
  • izz | grep Hello => you expect _ to contain the output if izz|grep Hello, right?

@caribpa
Copy link
Contributor Author

caribpa commented Feb 11, 2021

Ok so some questions:

* `izz` => you expect `_` to contain the output of `izz` ✅ 

* `izz~string` => you expect `_` to contain the output of `izz~string`, right? ✅ 

* `izz;_~string;_~string2;` => you expect `_` to contain the output of `izz` and keep its value even after executing `_~string`? ❎ 

* `izz | grep Hello` => you expect `_` to contain the output if `izz|grep Hello`, right? ✅ 

In izz;_~string;_~string2;, I expect the first _ to contain the output of izz, but the second _ should contain the output of _~string.
Like first writing izz ~string followed by Enter, and, on the next prompt: _ ~string2 followed by Enter.

@caribpa
Copy link
Contributor Author

caribpa commented Feb 11, 2021

I don't expect Rizin's _ to behave like bash/zsh !!, as !! gets expanded before even executing the line, and !! re-executes the last command, not just prints the output.

I expect _ to behave a little like $_ but showing the last output instead of retrieving the last parameter of the previously executed command.

Try the following in bash/zsh:

> echo hello; echo $_ world; echo hello $_
# And then the following:
> echo hello; echo !! world; echo hello !!

@ret2libc ret2libc removed this from the 0.2.0 milestone Mar 1, 2021
@stale stale bot added the stale label Sep 3, 2021
@ret2libc ret2libc removed the stale label Sep 3, 2021
@ret2libc
Copy link
Member

ret2libc commented Sep 3, 2021

The bug is still present. It now works when using |, but not when using ~.

@stale

This comment was marked as resolved.

@stale stale bot added the stale label Mar 8, 2022
@DMaroo DMaroo removed the stale label Mar 8, 2022
@stale stale bot added the stale label Sep 10, 2022
@rizinorg rizinorg deleted a comment from stale bot Sep 11, 2022
@stale stale bot removed the stale label Sep 11, 2022
@rizinorg rizinorg deleted a comment from stale bot Jan 8, 2023
@ret2libc ret2libc removed their assignment Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working shell test-required
Projects
Development

No branches or pull requests

5 participants