DEV Community

Cover image for 9 hidden gems of shell that beginners don’t know

9 hidden gems of shell that beginners don’t know

Rishiraj Purohit
・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 below examples/outputs/images when you run any of these commands.

  1. $PATH variable

    1. Your shell uses a variable called path to store all the directory and places separated by 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 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 few versions of php set up in my system and sometimes certain scripts require certain version of php.
    2. Using which 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 basically your home directory or a shorthand for /Users/rishirajpurohit. So to navigate to Sites folder in your home directory you can just use ~/Sites.

    Demo tilde character in bash

  4. - (dash character) : A dash represents the last place you were before coming to current directory and is very useful when you are jumping back and forth between directories.

    Demo dash character in bash

  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.

    Demo ls with path in bash

  6. Using < for Input : Not commonly used but sometimes a program might need large input and you may have it saved it 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 > ~/Sites/carz/sample_cars.json.

  8. Using >> for Append : Continuing the example from above, lets says 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 >> ~/Sites/carz/sample_cars.json.

    Demo io stream use in bash

  9. | (pipe character) : One of the most commonly used tricks to connect or chain programs 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

Thank you for reading and comment below your 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.

Discussion (0)