1

I'm trying to call the following within a bash script:

`history -a current_history`

it is supposed to create the file with the commands executed this session.

Works perfectly fine in a shell environment

Does not work within a bash script

I see information which shows that you must call history differently within a bash script, as the following format:

#!/bin/bash

HISTFILE=~/.bash_history  # Set the history file.
HISTTIMEFORMAT='%F %T '   # Set the hitory time format.
set -o history            # Enable the history.
file="/media/saleel_almajd/Study/linux/my_scripts/history.txt"
history >> $file          # Save the history.

but in doing so when modifying the last line to history -a.

this then returns no results when compared to the regularly called history -a file.

9
  • history -a ~/current_history
    – dlin
    May 1, 2020 at 11:51
  • doesnt work @dlin May 1, 2020 at 11:58
  • as i have said above, that command works fine OUTSIDE of a bash script, when it executed within a bash script it does not work. 4.15.0-99-generic @dlin May 1, 2020 at 12:02
  • What session's history do you want? The shell that is running the script, or the parent shell from which you executed the script?
    – terdon
    May 1, 2020 at 12:45
  • im not quite sure i understand what you mean, I want it to show the history of the current user, as it shows when calling the command natively via the shell @terdon May 1, 2020 at 12:59

3 Answers 3

0

If you just want to print the commands that were executed by your script inot a file using history -a, you can do this:

#!/bin/bash

set -o history            # Enable the history.
## Now set the HISTFILE to what you want
HISTFILE="/media/saleel_almajd/Study/linux/my_scripts/history.txt"
history -a                # write it to your file

This will result in the following being written to /media/saleel_almajd/Study/linux/my_scripts/history.txt:

$ cat /media/saleel_almajd/Study/linux/my_scripts/history.txt
## Now set the HISTFILE to what you want
HISTFILE="/media/saleel_almajd/Study/linux/my_scripts/history.txt"
history -a                # write it to your file

The HISTTIMEFORMAT variable is irrelevant. That is only used when the history file is read, not when it is printed to. You can still use it to see the history you printed. Just run this:

$ history -c  ## clear the current history
$ HISTFILE="/media/saleel_almajd/Study/linux/my_scripts/history.txt"
$ history -r  ## read the new one
$ HISTTIMEFORMAT='%F %T ' history
    1  2020-05-01 14:18:53 HISTFILE="/media/saleel_almajd/Study/linux/my_scripts/history.txt"
    2  2020-05-01 14:19:01 history -r
    3  2020-05-01 14:19:01 ## Now set the HISTFILE to what you want
    4  2020-05-01 14:19:01 HISTFILE="/media/saleel_almajd/Study/linux/my_scripts/history.txt"
    5  2020-05-01 14:19:01 history -a                # write it to your file
    6  2020-05-01 14:19:03 HISTTIMEFORMAT='%F %T ' history
2
  • im sorry, i dont understand where you got this from, i appreciate your effort. But i need a copy from the CURRENT session. Not from the script, not from the the total history list. History -a shows the current commands from your session, not total May 1, 2020 at 13:35
  • 1
    @throwaway56786897 no, history -a simply appends the commands of the current session. But if you run it in a script, the current session is the session running the script, scripts always run in their own shell instance. That's why I keep trying to get you to explain exactly what "session" you are referring to. I now think you want the history of the script's parent shell, of whatever shell you were in when you ran the script. Is that right? If so, why would you do this in a script instead of a function?
    – terdon
    May 1, 2020 at 14:27
0

Use a . dot or source before your script name, so it will be executed in the current shell:

$ . hist.sh

$ cat hist.sh 
history -a current_history

$ cat current_history 
cat curhist 
ls
rm cur*
ls
rm '\'
cat '\'
cd test
ls
rm current_history 
vim.tiny hist.sh 
. hist.sh 

This will place your current shell history into current_history file you've specified.

$ source --help
source: source filename [arguments]
    Execute commands from a file in the current shell.

If you want to save timestamps, you could try the next script:

HISTTIMEFORMAT='%c '
history -a current_history
paste -sd '#\n' current_history | awk -F"#" '{d=$2 ; $2="";print NR"\t"strftime("%d/%m/%y %T",d)"\t"$0}' > formatted_history
4
  • $HISTFILE slows all commands, not current commands when called for as: history -a current_history @Gryu May 1, 2020 at 13:49
  • @throwaway56786897 does sourcing work for you? It is for current session. If you don't need $HISTFILE, just don't use it. No one requires it.
    – Gryu
    May 1, 2020 at 14:03
  • after testing sourcing the .sh file works but, removes the formating from the command execution time & date provided by HITTIMEFORMAT @Gryu May 1, 2020 at 14:51
  • @throwaway56786897 it's not sourcing removes timestamps, but history -a somefile command does not take any attention to that variable)
    – Gryu
    May 1, 2020 at 15:39
0

In bash, to item "history -a" not working in a script, I can contribute some details.

Now I am using Kubuntu 22.04, but I suffer from this issue since many years.

1.

The script must be sourced (otherwise another .bah_history file is used but not that of the current shell). Then "history -a" does work normally -- with followig exception:

2.

In many such scripts, often before of "history -a" these two commands are used:

history -c

history -r

And of these two, it is

history -c

that causes the non-working of "history -a". Nothimg is written to .bah-history file.

regards

antondhidhdih

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .