(How) Is it possible to save a command in GNOME Terminal?

(How) Is it possible to ‘save’ a command that I regularly (with a small change) need to enter at the prompt, permanently in GNOME Terminal so that I don’t need to type the command every time when it’s needed and it’s still there after a shutdown or restart?

1 Like

By default this is already being done. The feature you want to research is called history.
A web search for “bash history” lead me to this page: https://www.howtogeek.com/44997/how-to-use-bash-history-to-improve-your-command-line-productivity/

Hello @coffee ,
You could also look at installing fzf since it is a very handy fuzzy search tool for commandline history. Also, not as a self plug, but I wrote a Bash Article for Fedora Magazine Customizing Bash - Fedora Magazine it does have some good info about Bash basics.

Ctrl + R will bring up recent history, so you can call back previous commands. Some people use fzf to have a more interactive prompt and other nice interactions.

I’m more interested in wether or not you would like to create a program, or have some other type of interaction. If you could be more specific, we could have other solutions more comfortable for you.

2 Likes

It’s not a big deal like a programme… It’s only about that I don’t need to type the same command every time.

When I press Ctrl + R, I get (reverse-i-search)':` as a new prompt. How do I go on to display the command I recently executed and that I’m looking for?

In addition to the shell history suggestions above, there are two other things you can do:

Bash aliases

I didn’t want temporary memory-backed filesystems showing up in the output of the df “disk free” command. Adding -x tmpfs -x devtmpfs' hides these. But that’s annoying to type every time. I also want to see the filesystem type -T, skip network filesystems -l, and have the output use “human friendly” units -h. To make this easy, I put the following into the file ~/.bashrc (which you should find already exists in your home directory):[1]

alias df='df -Tlh -x tmpfs -x devtmpfs'

Now, when I type df at the command prompt, the whole extended command is actually used. (To ignore any aliases and use the original command, prefix with \ — that is, literally enter \df instead of df as your command.)

The alias name doesn’t have to match an existing command — it could be diskfree or whatever.

Note that you’ll have to reload ~/.bashrc to make this take effect.

Write a shell script

This might sound intimidating, but … at the most basic level, it’s really accessible, because a shell script is just a list of commands.

  1. Create a regular text file (in nano, vim, VScode, GNOME Text editor, whatever)
  2. At the top of the file, put #!/bin/bash, which is a “magic” incandation which tells the Linux kernel that this file is to be processed by the bash shell.[2]
  3. Save the file somewhere in your PATH~/.local/bin/ is a common location these days. You can name it whatever you want (for example, ~/.local/bin/myscript). Ending with .sh is common, but not necessary (because it’s those magic bytes that really matter).
  4. Mark the file executable, like this: chmod +x ~/.local/bin/myscript
  5. Now, just run myscript, and … there you go!

  1. If it doesn’t, you can create it ↩︎

  2. You’ll see similar for Python programs, or for other shell and scripting languages. Other kinds of programs — binaries instead of scripts — start with different “magic”. For example, a typical Linux binary program like /bin/bash itself will start with ␡ELF ↩︎

4 Likes

Just start typing. The last command that contains the letters you typed will be filled in. You can then continue to press Ctrl + R to search the history backwards. When you find what you’re looking for, you can press Enter to execute the command or the (Right Arrow key) to print it to the prompt and modify it further.

2 Likes

I have already seen the command I was looking for so I know it exists but now it says (failed reverse-i-search). Does it perhaps search from back to front and when it reaches the beginning, it doesn’t continue searching from the end? How can the command be displayed again?

reverse-search-history is an incremental search, but not iterating through history. If I miss the command I’m looking for (which happens to me often), I just press Ctr + C to cancel the current search and then start over.

1 Like

I can’t find the command anymore. Is it possible when I found the command before and printed it to the default prompt with (Right Arrow key) and then deleted the command line with either the Backspace or the Delete key, that this deleted it from the reverse-search-history, too?

The reverse-search-history command sets the region to the matched text and activates the mark.
Hitting Enter accepts the line regardless of where the cursor is. If this line is non-empty, add it to the history list. If this line is a modified history line, then restore the history line to its original state.

I pressed Ctrl + R at the default prompt ($), (reverse-i-search)‘: was displayed and I pressed Ctrl + R a few times. Then I found the command I was looking for. I pressed the (Right Arrow key) to print it to the prompt ($). After that I deleted the command line by pressing Backspace and/or Delete. As I pressed Ctrl + R again, I couldn’t find the command (that I previously found) anymore at (reverse-i-search)’:. What’s the reason for this?

I strongly think you should try fzf sudo dnf install fzf since it gives you a way to search your command history with regex if you want or partial command names even, then retrieves a list of command line history items that meet the search criteria you entered. It’s much easier than what you seem to be going through atm.

A good read about setting up fzf on redhat or fedora … https://www.redhat.com/sysadmin/fzf-linux-fuzzy-finder

1 Like

It seems to be the way that history works — you are actually inserting a blank line in place of the deleted command in the current history list. The same applies if you use the up/down keys to scroll through the history.
However, when you close the current Bash session and open a new one, the missing command is appended to the history file and is available again.

1 Like

Don’t get me started on fzf usefulness ! :party:

I agree, fzf, eza and bat are a must set of commandline tools.

That’s right.

Just as a quick aside to this conversation :

Having

PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

inside of your .basrc file will help you, if you decide to use more than 1 Terminal while you work. The commands will be saved across sessions, and you will not lose any previously entered commands from other Terminals open.

1 Like