You are not logged in.

#1 2023-10-08 20:29:08

Tx86
Banned
Registered: 2021-07-24
Posts: 39

Execute previous command inside of a bash script

So, we all know that !! will execute the previous command from the Command Line.
We also know that !-n will execute the previous whatever-th command, where "n" is a number, from the Command Line.

But neither of these work inside of a bash script. How would I go about doing this inside of a bash script?

Offline

#2 2023-10-08 20:44:15

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 12,042
Website

Re: Execute previous command inside of a bash script

Smells like an x-y question, but: if you really want to run the same command multiple times in a row, use a loop. If you want to run the same command (or set of commands) from earlier, put it in a function and call that.


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

#3 2023-10-08 20:59:34

Tx86
Banned
Registered: 2021-07-24
Posts: 39

Re: Execute previous command inside of a bash script

WorMzy wrote:

Smells like an x-y question, but: if you really want to run the same command multiple times in a row, use a loop. If you want to run the same command (or set of commands) from earlier, put it in a function and call that.

That would be the obvious solution, but for this I need to call back to commands executed on a timeline, not by function names.

Offline

#4 2023-10-08 22:31:44

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 12,042
Website

Re: Execute previous command inside of a bash script


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

#5 2023-10-08 22:39:10

Tx86
Banned
Registered: 2021-07-24
Posts: 39

Re: Execute previous command inside of a bash script

WorMzy wrote:

Because I need "Command 2" to go back to the original command that executed it, which can come from several other different commands.

Offline

#6 2023-10-09 00:05:18

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,750
Website

Re: Execute previous command inside of a bash script

Stop giving awkwardly vague descriptions - post your actual code.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#7 2023-10-09 01:52:51

Tx86
Banned
Registered: 2021-07-24
Posts: 39

Re: Execute previous command inside of a bash script

Trilby wrote:

Stop giving awkwardly vague descriptions - post your actual code.

I'll post code if you can actually answer the original question. Without referring to a specifically named function, can previously executed commands be recalled in a fashion similar to using !-n inside of a bash script, or not? Because if it's not possible, I'll fix it a different way, I just didn't want to.

Last edited by Tx86 (2023-10-09 01:53:10)

Offline

#8 2023-10-09 02:02:06

Scimmia
Fellow
Registered: 2012-09-01
Posts: 11,714

Re: Execute previous command inside of a bash script

Let me guess, this is related to package management and you don't want to post it because it's not actually Arch Linux?

Offline

#9 2023-10-09 02:03:53

Tx86
Banned
Registered: 2021-07-24
Posts: 39

Re: Execute previous command inside of a bash script

Scimmia wrote:

Let me guess, this is related to package management and you don't want to post it because it's not actually Arch Linux?

No. I mean, you're right, I'm not actually using Arch right now, but that's not why I want to post it. I just want the question answered.

Offline

#10 2023-10-09 02:13:51

Scimmia
Fellow
Registered: 2012-09-01
Posts: 11,714

Re: Execute previous command inside of a bash script

Tx86 wrote:
Scimmia wrote:

Let me guess, this is related to package management and you don't want to post it because it's not actually Arch Linux?

No. I mean, you're right, I'm not actually using Arch right now, but that's not why I want to post it. I just want the question answered.

And yet https://terms.archlinux.org/docs/code-o … pport-only

Offline

#11 2023-10-09 02:16:22

Tx86
Banned
Registered: 2021-07-24
Posts: 39

Re: Execute previous command inside of a bash script

It's literally just a bash script.

Offline

#12 2023-10-09 02:37:41

solskog
Member
Registered: 2020-09-05
Posts: 426

Re: Execute previous command inside of a bash script

Just append -i option inside your bash script.

$:  cat /usr/local/bin/history.verify
#!/usr/sbin/bash -i
date;
id;
fc -l
!!
!-2

Offline

#13 2023-10-09 02:43:56

Tx86
Banned
Registered: 2021-07-24
Posts: 39

Re: Execute previous command inside of a bash script

solskog wrote:

Just append -i option inside your bash script.

$:  cat /usr/local/bin/history.verify
#!/usr/sbin/bash -i
date;
id;
fc -l
!!
!-2

Thanks for the effort, but it unfortunately doesn't work. It's fine. I'll just do it the hard way, even though I really didn't want to. But thank for actually giving me an answer instead of dancing around the question.

Offline

#14 2023-10-09 03:21:44

solskog
Member
Registered: 2020-09-05
Posts: 426

Re: Execute previous command inside of a bash script

This works in a normal Linux distro, if your distro or enviroment are special. Please let us known, so that people can help you.

Offline

#15 2023-10-09 03:22:39

Tx86
Banned
Registered: 2021-07-24
Posts: 39

Re: Execute previous command inside of a bash script

solskog wrote:

This works in a normal Linux distro, if your distro or enviroment are special. Please let us known, so that people can help you.

It's not. Again, thank you for your time.

Offline

#16 2023-10-09 03:47:24

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,425
Website

Re: Execute previous command inside of a bash script

Topic closed...

Offline

Board footer

Powered by FluxBB