Jump to navigation Jump to search

BASH and Octave

114 bytes removed, 15:02, 13 June 2019
Improve markup.
One of the nice features of Octave (and similar languages) is that you don't have to compile the code that you generate. It is interpreted by Octave instead. This make learning the language easy by trial-and-error. Very often it is useful to save what you have learned from you fiddlings during the current Octave 'session' that you have opened for this fiddling.
Octave keeps a copy of all your command you give on the Octave command line. In Linux it is saved in the file {{CodelinePath|~/.octave_hist}}.
If you want to save all the Octave commands you gave during the last Octave session, generate a file {{CodelinePath|octave_save_last_session}} in your directory {{CodelinePath|~/bin}} and put the following inside:
(that is: you have to open a terminal on your Linux computer and type the following commands on the terminal command line (make sure it is the BASH shell).
{{Code||<syntaxhighlight lang="bash" style="font-size:13px">
tac $HOME/.octave_hist | awk '/^#/ {found++} ; found<2 ' |tac >$filename
Then you still need to tell the BASH shell to make this file {{CodelinePath|~/bin/octave_save_last_session}} executable:
{{Code||<syntaxhighlight lang="bash" style="font-size:13px"> chmod u+x ~/bin/octave_save_last_session</syntaxhighlight>}}
Now, imaging you have 'played around' in an interactive Octave session to try/test something: you want to save the commands you just used to not-forget. Just close the Octave session. If (in Linux) you close Octave that you have started from the command line, you will automatically return to the command line. Now you want to have a seperate copy of your last session put in a file (a {{Codeline|*.m}} file). On the command line, you just type {{Codeline|octave_save_last_session}}, and the last session (only!) is copied from the {{CodelinePath|~/.octave_hist}} file and saved to {{CodelinePath|octave_save_last_session.m}}. You can edit that file to remove parts you don't want to save, or you can re-execute the commands by {{Codeline|<code>octave --persist octave_save_last_session.m}}</code>.
[[Category:Tips and tricks]]

Navigation menu