In order to write bash-scripts, I often do the task manually to see how it works. I type ### at the start of my session.
The function fetches the commands from the last occurrence of '###', excluding the function call. You could prefix this with a here-document to have a proper script-header.
Delete some lines, add a few variables and a loop, and you're ready to go.
This function could probably be much shorter...

Bash's history expansion character, "!", has many features, including "!:" for choosing a specific argument (or range of arguments) from the history. The gist is any number after !: is the number of the argument you want, with !:1 being the first argument and !:0 being the command. See the sample output for a few examples. For full details search for "^HISTORY EXPANSION" in the bash(1) man page.
Note that this version improves on the previous function in that it handles arguments that include whitespace correctly.
Show Sample Output

[Click the "show sample output" link to see how to use this keystroke.]
Meta-p is one of my all time most used and most loved features of working at the command line. It's also one that surprisingly few people know about. To use it with bash (actually in any readline application), you'll need to add a couple lines to your .inputrc then have bash reread the .inputrc using the bind command:
echo '"\en": history-search-forward' >> ~/.inputrcecho '"\ep": history-search-backward' >> ~/.inputrcbind -f ~/.inputrc
I first learned about this feature in tcsh. When I switched over to bash about fifteen years ago, I had assumed I'd prefer ^R to search in reverse. Intuitively ^R seemed better since you could search for an argument instead of a command. I think that, like using a microkernel for the Hurd, it sounded so obviously right fifteen years ago, but that was only because the older way had benefits we hadn't known about.
I think many of you who use the command line as much as I do know that we can just be thinking about what results we want and our fingers will start typing the commands needed. I assume it's some sort of parallel processing going on with the linguistic part of the brain. Unfortunately, that parallelism doesn't seem to work (at least for me) with searching the history. I realize I can save myself typing using the history shortly after my fingers have already started "speaking". But, when I hit ^R in Bash, everything I've already typed gets ignored and I have to stop and think again about what I was doing. It's a small bump in the road but it can be annoying, especially for long-time command line users. Usually M-p is exactly what I need to save myself time and trouble.
If you use the command line a lot, please give Meta-p a try. You may be surprised how it frees your brain to process more smoothly in parallel. (Or maybe it won't. Post here and let me know either way. ☺)
Show Sample Output

This is sample output - yours may be different.

### Start typing a command, say "nm",
$ nm # Realize you've done this before, so press M-p (hold ALT and hit P)
$ nm /usr/bin/wine # Bash completes the line from history! Yay!
### Not the one you wanted? Just keep hitting M-p until you find the one you want.
$ nm /usr/bin/wineserver
### Need to be more specific? Note that the cursor is still after the "nm" that we typed at first, so if we hit "a" it shows this:
$ nma /usr/bin/wineserver
### ...but then, another press of M-p and, voila, we get:
$ nmap -sUT -p- localhost
### Just go ahead and hit enter (you don't need to be at the end of the line) and it runs:
Starting Nmap 7.21 ( http://nmap.org ) at 2018-02-29 09:20 XGT
Interesting ports on localhost (127.0.0.1):
Not shown: 131045 closed ports
PORT STATE SERVICE
22/tcp open ssh
25/tcp open smtp
80/tcp open http
[...]

This gets the Nth argument in the last line of your history file. This is useful where history is being written after each command, and you want to use arguments from the previous command in the current command, such as when doing copies/moving directories etc.
I wrote this after getting irritated with having to continually type in long paths/arguments.
You could also use $_ if all you want is the last argument.
Show Sample Output

This is sample output - yours may be different.

$ echo "This little piggy went to the market"
This little piggy went to the market
$ garg 5
went

I know how hard it is to find an old command running through all the files because you couldn't remember for your life what it was. Heres the solution!! Grep the history for it. depending on how old the command you can head or tail or if you wanted to search all because you cannot think how long ago it was then miss out the middle part of the command. This is a very easy and effective way to find that command you are looking for.

just an alternative to setting the size, this allows you to scroll up and see your previous commands in a given session but when you logout the history is not saved. That's the only advantage to doing it this way..
Show Sample Output

This is sample output - yours may be different.

export HISTFILE=/dev/null
exit (won't try to save history until you logout)

Only from a remote machine:
Only access to the server will be logged, but not the command.
The same way, you can run any command without loggin it to history.
ssh user@localhost will be registered in the history as well, and it's not usable.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.
That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and
voted up or down.

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted
to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning,
there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.