9 hidden gems of shell that beginners don’t know

9 hidden gems of shell that beginners don’t know

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.

  1. $PATH variable

    1. 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.

    2. You can update the path dynamically to add or replace a directory when running a program

    3. It can also help in finding the directory of certain programs.

    4. 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.

      demo path variable in bash

  2. Which program

    1. 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.

    2. 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.

      demo which program in bash

  3. ~ (tilde character): Tilde is your home directory or shorthand for /Users/rishirajpurohit. So to navigate to Sites folder in your home directory you can just use ~/Sites.

  4. - (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.

  5. ls with path : ls takes 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.

  6. 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.

  7. 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.

  8. 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.

  9. | (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 docker will 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 Dev by becoming a sponsor. Any amount is appreciated!