[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: what's $_ in bash




On 4/7/23 23:40, davidson wrote:
On Sat, 8 Apr 2023 tom@myposts.ovh wrote:
Hello

in bash shell, what's "$_" variable?


kent@westk-9463:~$ ls *html
morsekeyer.html  morse.html  myGameEasier.html  myGame.html

The 'ls *html' "expands" to "ls morsekeyer.html morse.html myGameEasier.html myGame.html", which means that the last item to that last command was "myGame.html", as we see below from echoing "$_".

kent@westk-9463:~$ echo The last item on the command-line above, after expansion, was -- $_.
The last item on the command-line, after expansion, was -- myGame.html.



Below, "*kent" does not expand any further, so "*kent" gets returned by the "$_".

kent@westk-9463:~$ ls *kent
ls: cannot access '*kent': No such file or directory
kent@westk-9463:~$ echo The last item on the command-line above, after expansion, was -- $_.
The last item on the command-line, after expansion, was -- *kent.



And below is a final example:

kent@westk-9463:~$ ls *conf
syslog.conf

kent@westk-9463:~$ echo The last item on the command-line above, after expansion, was -- $_.
The last item on the command-line, after expansion, was -- syslog.conf.



Reply to: