9 hidden gems of shell that beginners don’t know
3 min read
While I ran these commands in macOS using iterm2, the tricks are valid for most of the terminals out there.
Quick side note: My username is rishirajpurohit but it will be your username for the below examples/outputs/images when you run any of these commands.
Your shell uses a variable called the path to store all the directories and places separated by a colon where it can look for a program to run.
You can update the path dynamically to add or replace a directory when running a program
It can also help in finding the directory of certain programs.
Most programs, if not done automatically, will require you to add their executable to the path so that you can directly use them from the shell.
Sometimes you want to see which version or what program will run when you type a command. For example, I have a few versions of PHP set up in my system and sometimes certain scripts require a certain version of PHP.
Using this I can quickly see the path of the PHP that will run my script and change it if needed using the $PATH variable mentioned above.
~ (tilde character): Tilde is your home directory or shorthand for
/Users/rishirajpurohit. So to navigate to
Sitesfolder in your home directory you can just use
- (dash character): A dash represents the last place you were before coming to the current directory and is very useful when you are jumping back and forth between directories.
ls with path :
lstakes a path as an argument and can list the contents of the path you provide, so you don’t have to cd to a folder to see its contents. By default, the path is set to
.or your current directory.
Using < for Input: Not commonly used but sometimes a program might need large input and you may have it saved in a file, you can use the < character to pass the contents of the file as input to the program.
Using > for Output: A lot of times we want to save the output of a program to a file instead of displaying it in a prompt. For example,
python fetch_results.py > ~/Sites/carz/sample_cars.json.
Using >> for Append: Continuing the example from above, let's say you want to run the command every few hours to fetch additional data, and want to just append the result to the same file, you can use >> character. For example,
python fetch_results.py >> ~/Sites/carz/sample_cars.json.
| (pipe character): One of the most commonly used tricks to connect or chain programs is when you want the output of a program as an input of another program.
To find all files containing .py :
ls | grep .py.
To find a command you ran in the past:
history | grep dockerwill return the command.
![Demo pipe character in bash](https://dev-to-uploads.s3.amazonaws.com/i/wpeo5jk9iqteaq5ikukc.png align="left")
Thank you for reading and commenting below your the tools and tricks that you learned over time and hope that you knew when starting.
Also give claps, shouts, unicorns or anything else if you liked the post.
Did you find this article valuable?
Support Rishiraj Purohit by becoming a sponsor. Any amount is appreciated!