Difference between revisions of "Bash command codes"

From UVOO Tech Wiki
Jump to navigation Jump to search
(Created page with "https://stackoverflow.com/questions/6109225/echoing-the-last-command-run-in-bash")
 
 
Line 1: Line 1:
 
https://stackoverflow.com/questions/6109225/echoing-the-last-command-run-in-bash
 
https://stackoverflow.com/questions/6109225/echoing-the-last-command-run-in-bash
 +
```
 +
Bash has built in features to access the last command executed. But that's the last whole command (e.g. the whole case command), not individual simple commands like you originally requested.
 +
 +
!:0 = the name of command executed.
 +
 +
!:1 = the first parameter of the previous command
 +
 +
!:4 = the fourth parameter of the previous command
 +
 +
!:* = all of the parameters of the previous command
 +
 +
!^ = the first parameter of the previous command (same as !:1)
 +
 +
!$ = the final parameter of the previous command
 +
 +
!:-3 = all parameters in range 0-3 (inclusive)
 +
 +
!:2-5 = all parameters in range 2-5 (inclusive)
 +
 +
!! = the previous command line
 +
 +
etc.
 +
 +
So, the simplest answer to the question is, in fact:
 +
 +
echo !!
 +
...alternatively:
 +
 +
echo "Last command run was ["!:0"] with arguments ["!:*"]"
 +
Try it yourself!
 +
 +
echo this is a test
 +
echo !!
 +
In a script, history expansion is turned off by default, you need to enable it with
 +
 +
set -o history -o histexpand
 +
```

Latest revision as of 21:39, 30 August 2022

https://stackoverflow.com/questions/6109225/echoing-the-last-command-run-in-bash

Bash has built in features to access the last command executed. But that's the last whole command (e.g. the whole case command), not individual simple commands like you originally requested.

!:0 = the name of command executed.

!:1 = the first parameter of the previous command

!:4 = the fourth parameter of the previous command

!:* = all of the parameters of the previous command

!^ = the first parameter of the previous command (same as !:1)

!$ = the final parameter of the previous command

!:-3 = all parameters in range 0-3 (inclusive)

!:2-5 = all parameters in range 2-5 (inclusive)

!! = the previous command line

etc.

So, the simplest answer to the question is, in fact:

echo !!
...alternatively:

echo "Last command run was ["!:0"] with arguments ["!:*"]"
Try it yourself!

echo this is a test
echo !!
In a script, history expansion is turned off by default, you need to enable it with

set -o history -o histexpand