The concept was, if you wanted some examples of grep that were easier examples than diving through a man page, you could use curl and at the command line, type curl https://cheat.sh/grep and in the blink of an eye, you would get
# grep
# Matches patterns in input text.
# Supports simple patterns and regular expressions.
# Search for an exact string:
grep search_string path/to/file
# Search in case-insensitive mode:
grep -i search_string path/to/file
# Search recursively (ignoring non-text files) in current directory for an exact string:
grep -RI search_string .
# Use extended regular expressions (supporting `?`, `+`, `{}`, `()` and `|`):
grep -E ^regex$ path/to/file
# Print 3 lines of [C]ontext around, [B]efore, or [A]fter each match:
grep -C|B|A 3 search_string path/to/file
# Print file name with the corresponding line number for each match:
grep -Hn search_string path/to/file
# Use the standard input instead of a file:
cat path/to/file | grep search_string
# Invert match for excluding specific strings:
grep -v search_string
which is very useful!!
So, what I wanted to do was create an alias so I could just type cheat grep and get the same results…
However, that won’t work because of the space (it would send curl https://cheat.sh/ grep) which results in the cheat.sh main page and then curl: (6) Could not resolve host: grep because of the space…
So I tried to make a bash function…
function cheat { curl https://cheat.sh/$1 }
which gives me the same error…
So how can I create a function/alias for this to work… and if it’s a bash function, where should I put it (it’s own file or a custom ‘bash scripts’ file that has more than this), and where can I link it so that it always runs when I type cheat from the command line (I was originally thinking bash rc - but then just like it’s better to put custom aliases in their own file, I figured I should put custom small functions in their own file as well)
Unfortunately this does not work for me with Ubuntu Mate (20.04)… I have to sudo to put it into /usr/bin and then when I run cheat it says permission denied, and if I run sudo cheat, it says it can’t find the command.
I remember one of the command line tips/tricks from DL was !! which was to repeat the last command… so if your forgot, for example, the sudo command, on say apt update you could do sudo !! and it would be the same as running sudo apt update…
An example that they gave an alias for it… alias please='sudo !!'
So I put that command in my bash_aliases file, and when I run it, I get sudo: !!: command not found
I’m not sure why this doesn’t work for me… because if I manually type sudo !! after running apt update it works as expected…
Something like this should work even if the command has standard input, output redirection:
alias please=‘sudo “$BASH” -c “$(history -p !!)”’
It seems that Bash has to expand !! directly and it is not done through alias substitution. Section 9.3 of the below website has a more in depth treatment of History Expansion if you are interested:
Learning moment… the reason I thought it was the quotes is because I tried it right after using my test command. Without the quotes the interpreter runs everything beyond the “=” so sudo !! dragged my test command into the assignment prior to it being assigned. The interpreted version even shows up in the terminal history instead of the one I wrote. That meant whenever I ran the alias it outputted my test rather than a sudo of the previous command making me think it worked.