Linux Basics – Operators
In this post we will talk about some operators most commonly used to interact with programs, ” > , >> , & , && and $”
“>” is the operator for output redirection. Meaning that you can redirect the output of any command to a file. For example if I were to run echo hello > file, then instead of outputting hello to the console, it would save that output to a file called file. Echo is as it sound it will echo any thing you type back to screen
It is worth noting that if you were to use this operator on a file that already exists, it would completely erase the contents of that file and replace it with the output from your command
Q:- How would you output twenty to a file called test? echo twenty > test
“>>” does mainly the same thing as >, with one key difference. >> appends the output of a command to a file, instead of erasing it.
&& means as you might expect “and”. Meaning && allows you to execute a second command after the first one has executed successfully. Meaning
ls && echo hello will work fine, but
sdhkljfh && echo hello will fail.
Note: Since the second command happens after the first command, you can use something created in the first command in the second command.
Much unlike &&, & has nothing to do with and at all(try saying that 10 times fast). & is a background operator, meaning say you run a command that takes 10 seconds to run, normally you wouldn’t be able to run commands during that period; however, with & that command will still execute and you’ll be able to run other commands.
The $ is an unusually special operator, as it is used to denote environment variables. These are variables set by the computer(you can set them yourself but we’ll get into that) that are used to affect different processes and how they work. Meaning that if you edit these variables you can change how certain processes work on your computer. For example your current user is always stored in an environment variable called $USER. You can view these variables with the echo command.
Naturally this means environment variables can be used as input for other commands as well, for example say I wanted to create a file which is the name of our current user, I could do
Environment variables can also be set pretty easily, just running
export <varname>=<value> will set that as an environment variable
Q:- How would you set nootnoot equal to 1111 ?
Q:- What is the value of the home environment variable?
Hope you got these operator, keep learning linux , happy hacking
Any questions comment below.