I'm trying to get last executed command from command line from a script to be saved for a later reference:
Example:
# echo "Hello World!!!"
> Hello World!!!
# my_script.sh
> echo "Hello World!!!"
and the content of the script would be :
#!/usr/bin/ksh
fc -nl -1 | sed -n 1p
Now as you notices using here ksh and fc is a built in command which if understood correctly should be implemented by any POSIX compatible shells. [I understand that this feature is interactive and that calling same fc again will give different result but this is not the concern do discuss about]
Above works so far so good only if my_script.sh is being called from the shell which is as well ksh, or if calling from bash and changing 1st line of script as #!/bin/bash then it works too and it doesn't if shells are different.
I would like to know if there is any good way to achieve above without being constrained by the shell your script is called from. I understand that fc is a built in command and it works per shell thus most probably my approach is not good at all from what I want to achieve. Any better suggestions?
history? Don't have much time to check this myself but might be worth a shot...bashhistory fromkshand vice versa requires that each shell is saving its history immediately and that the other shell knows the name of that file. Otherwise, history is kept in memory and there is no way for one shell to query the in-memory history of another.my_toolwill be used to save not only one command but any one I'll decide to add to the list of commands and I want to avoid spoiling any.rcfile every time I need to add/change/delete one of my command/path.sourcefiles in a directory every time something in it updates, you can do that... with a helper defined once in your.bashrc. :)