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

Implement $_ , the last argument to the previous command run #262

Open
jyn514 opened this issue Mar 20, 2019 · 5 comments
Open

Implement $_ , the last argument to the previous command run #262

jyn514 opened this issue Mar 20, 2019 · 5 comments

Comments

@jyn514
Copy link
Contributor

jyn514 commented Mar 20, 2019

bash treats $_ as 'the last argument to the previous command run`. I think it's unset for the first command. osh treats this as the name it was invoked with.

Expected behaviour:

$ ls INSTALL.txt 
INSTALL.txt
$ echo $_
INSTALL.txt

Actual behaviour:

$ ls INSTALL.txt
INSTALL.txt
$ echo $_
bin/osh
@andychu
Copy link
Contributor

andychu commented Mar 20, 2019

OK it looks like OSH does nothing, but it's inherited from the parent bash process! bash actually exports it for some reason.

Thanks for the report, keep them coming!

(Although personally I've never used $_, I always use !$ i.e. history expansion)

@andychu andychu changed the title Treat $_ properly Implement $_ , the last argument to the previous command run Mar 20, 2019
@Crestwave
Copy link
Contributor

Crestwave commented Mar 28, 2019

(Although personally I've never used $_, I always use !$ i.e. history expansion)

$_ is usually used as a temporary variable for parameter expansion in scripts. For example, to print the effective username, you could run the following:

: '\u'
printf '%s\n' "${_@P}"

@andychu
Copy link
Contributor

andychu commented Apr 7, 2020

OK I just wrote some tests, and this is implemented in both bash and zsh. For some reason I thought it was only an interactive thing but it's not.

#679 depends on it

@andychu
Copy link
Contributor

andychu commented Apr 14, 2020

FWIW after looking at the neofetch diff, I'm going to concentrate on the word eval quoting problems over this one...

There were only 2 usages -- it seems like the first was definitely questionable since you can use $1, and the second was also questionable since you can just have os= in every case clause.


An ulterior motivation for not doing this is that I wanted to make $_ mean something else, e.g. maybe something like what it means in Perl.

In other words, I'm sort of biased against this feature, since I think it makes programs harder to read, and the 10K lines of neofetch didn't dissuade me of that...

Still if anyone wants to submit a patch, there are already test cases for it...

@Crestwave
Copy link
Contributor

It is true that its uses are totally replaceable. However, the thing is that it is used (it's used several times in the pure bash bible, so even others might use it) and scripts will have to manually support Oil. I suppose it could be fine if @dylanaraps agrees not to use it, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants