DEV Community

Cover image for 7 Linux operators you should know about
Bassem
Bassem

Posted on

7 Linux operators you should know about

Hey devs from all over the world 😊

Okay! I know I am a Frontend developer, but I really like Linux. Just hear me out!

Today, I want to tell you guys about some Linux shell operators that you absolutely can't live without while chaining commands. 🤯

Going forward, we need an example of a command to explain the operators on. Due to our current situation and my absolute hatred, I am using this one :

    kill covid19 --force 
Enter fullscreen mode Exit fullscreen mode

I know it won't work! but, maybe because no one tried it before. 🙄

Let's dive right in, 🌊

Are you after the output?

Starting off, We have the write operator > This one is used to write the output to a file. For example,

    kill covid19 --force > res.txt
Enter fullscreen mode Exit fullscreen mode

Wait a sec! This > will overwrite the content of the file. If you care about res.txt you will have to use the append >> operator which will append the output to the content of the file.

Actually, The output operator > is just a shorthand for 1>

Is there a 2>? Yes!

Is there a 3>? Don't get carried away!✋

1> is for standard program output, while 2> is for error output. For example,

    kill covid19 --force 1> res.txt 2> err.txt
Enter fullscreen mode Exit fullscreen mode

What if your program requires some input file?

I know you guessed it! Just flip it over <

    kill covid19 --force < doctorsList.txt
Enter fullscreen mode Exit fullscreen mode

One command is not enough.

Okay, Let's say it's not that easy. What if we need more? You can chain commands one after the other using the AND operator && like this :

    kill covid19 --force && rebuild
Enter fullscreen mode Exit fullscreen mode

This && is an AND operator. It will only run the next program if the previous one exited successfully.

We also have an OR operator ||

    kill covid19 || (I honestly don't know what to write here! 🤯)
Enter fullscreen mode Exit fullscreen mode

This one will only run the next program if the previous one failed ( Let's hope It doesn't ).

Is that it?

No! We still have more.

Killing covid19 is gonna take some time 😔 We can use the ampersand & at the end to make it run in the background. Leaving the terminal free to run any other programs like this :

    kill covid19 & 
Enter fullscreen mode Exit fullscreen mode

Wait! I am saving the best for last. The most awesome pipe operator | This one can pass the output of one program to the next. Let's try to count the number of lines killing covid19 will output. (cause why not!)

    kill covid19 | wc -l
Enter fullscreen mode Exit fullscreen mode

That's it for now. If you guys liked it, please let me know down in the comments section.

You could also check my Know Your Linux series here on dev.to.

Also, I have a small recommendation -unpaid- for you guys, I have been hacking away my quarantine days and you could too. Check out tryhackme.com. It is super cool!

If you want to know more or if you would like a hacking wingman, hit me up on discord at Bassem Mohamed#3921

As always,
Happy coding 🔥🔥
“كود بسعادة”

Top comments (0)