An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
My Favorite Bash Tips, Tricks, and Shortcuts
If you're on DEV, chances are you spend at least some time in the terminal, maybe even a lot of time.
Over the years, I've picked up a number of tips and tricks from fellow developers. Almost every time I pair program with someone new, chances are I'll notice them doing something neat and ask them how they did it.
Here are some of my favorites.
I use bash as my default terminal, but most of these tips translate to other terminals as well.
Note: This post isn't meant to teach the basics of using the terminal. There are many great resources online (I remember doing Codecademy's Command Line course when I was starting out.
The -
operator
Do you find yourself switching back and forth between two directories often?
You can use cd -
to change to the last directory you were in like this:
~ $ cd directory1~/directory1 $ cd directory2~/directory2 $ cd -~/directory1 $
This also works with git when switching between branches:
~/my-project(main)$ git checkout feature-branch~/my-project(feature-branch)$ git checkout -~/my-project(main)$
The !!
operator
This happens a lot!
You type a command, only to get a "Permission denied" so you have to retype the command again, this time using sudo
.
The !!
operator echoes the last command you typed into your terminal.
You can use it like this:
$ some-dangerous-script.sh=> Error: Permission Denied$ sudo !!=> Enter password for some-dangerous-script.sh:
{curly brace expansion}
If you ever need to run a series of very similar commands that differ by just a few characters (like for example, if you want to create a few filenames with lightly different extensions) you can use the characters that will be different between two curly braces and the command will run once for each one.
Like this:
$ touch file-{1,2,3}.md$ ls=> file-1.md file-2.md file-3.md
You can also pass in a range:
$ touch file-{1..3}.md$ ls=> file-1.md file-2.md file-3.md
Search using Ctrl+R
Ae you like me? Would you press the up button 20 times to avoid typing out a 7 character command?
This next one was a lifesaver for me!
You can type Ctrl + R (Cmd + R on a mac) followed by the first few letters of the command you want to search through your bash history and bring up the command you need.
(Sorry, I can't think of how to demonstrate that with a code snippet. Just go to your terminal, type in Ctrl + R and start typing).
Aliases
Aliases are a great way to save time and keystrokes. If there's a command or a series of commands you find yourself typing often, it's making an alias can be very helpful.
In order to set aliases, first open the ~/.bashrc
file in your favorite editor and check if it has the following lines in it:
if [ -f ~/.bash_aliases ]; then . ~/.bash_aliasesfi
It should be there already, if it isn't just add it to the bottom of the file.
Next open ~/.bash_aliases
in your editor (or create it if it doesn't exist) and add your aliases in the following format:
alias something="definition"
Some playful aliases I have in my .bash_aliases
are:
alias please="sudo "alias yeet="rm -rf
I also have a number of functions defined there, for more complex command series:
mk() { mkdir $1 && cd $1}gclone() { git clone "$1" && cd "$(basename "$1" .git)"}
The mk
alias takes a directory name as an argument, mk
s the directory and then cd
s into it.
The gclone
alias takes a git repo, cloned it, and then cd
s into it.
After adding aliases to your .bash_aliases
they should load automatically every time you start a new terminal session.
If you would like to use your aliases in your current session, run:
source ~/.bash_aliases
That's what I can think of for now.
Do you have any favorite tips and tricks?
Please please do share them! I always love learning new ones!
Original Link: https://dev.to/yechielk/my-favorite-bash-tips-tricks-and-shortcuts-36bj
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To