Skip to content

Instantly share code, notes, and snippets.

@rubbsdecvik
Last active March 8, 2017 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubbsdecvik/219450c985e845f5e17b0827f87a9a54 to your computer and use it in GitHub Desktop.
Save rubbsdecvik/219450c985e845f5e17b0827f87a9a54 to your computer and use it in GitHub Desktop.
Bash Tips and Tricks

MORE TRICKS

I have another document with some other tricks we didn't cover last night here: https://gist.github.com/rubbsdecvik/431adebd1daa2aecfcc333d3a433e9c4

You can use !! to substitute a previous command

For example:

Run a command.

input:

whoami

output:

pregan

Then use !! to substitute:

input:

sudo !!

output:

root

Use ^search^replace to substitute a string from previous command

Run a command:

input:

echo foo

output:

foo

Then use ^foo^bar to swap the string

input:

^foo^bar

This will change the command to echo bar.

output:

bar

Fix commands with fc

You can put a command into your editor with fc which will put the previous command into an editing session. Once you save and quit, it will execute the contents of your editor buffer.

Use Pipeviewer to see a progress bar

First you will need to install pv

On Fedora systems:

sudo dnf install pv

On Red Hat systems:

sudo yum install pv

On Debian/Ubuntu:

sudo apt install pv

Then you can use it anywhere you can use cat but the stderr will now contain a progress bar.

For example:

pv Downloads/Fedora-Workstation-Live-x86_64-24-1.2.iso > /dev/null

Use !$ to reuse the last argument in a command

Run a command

input:

mkdir devops

Then use `!$! to get the last "word" from the previous command

input:

cd !$

This will change directories to devops

Use Grep's options brackets to avoid an extraenous grep -v

When you are grepping through a list of ps output, you will match on the very search you're making.

input:

ps aux | grep bash

output:

pregan    6925  0.0  0.0 133788  4976 pts/13   Ss   Feb17   0:00 bash
pregan    7672  0.0  0.0 119376   960 pts/11   S+   12:47   0:00 grep --color=auto bash
pregan    9390  0.0  0.0 134304  5672 pts/7    Ss+  Mar02   0:00 -bash

You can remove that by using [] within your grep search.

input:

ps aux | grep [b]ash

output:

pregan    6925  0.0  0.0 133788  4976 pts/13   Ss   Feb17   0:00 bash
pregan    9390  0.0  0.0 134304  5672 pts/7    Ss+  Mar02   0:00 -bash

Aliases can be useful for common commands

You can use alias to see a list of your aliases and then how to use the alias command to create new ones.

One useful one is:

alias bel='tput bel'

After this alias has been created, you then can use the command bel to make your terminal make a noise.

Use tmux to have a disconnectable terminal multiplexer

Tmux is a multiplexer, which just means it can act as multiple terminals inside the same terminal session. This is very helpful if you are connected via ssh. You can learn more about Tmux here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment