The command line is used for navigating through files in our filesystem and making changes to those files, if we wish to. However, the command line is so powerful that it can be used for more than that.
When we run some specific commands in the command line we can receive a stream of output in the terminal. Practically we can type a string of words, press enter and see those words return back to us!
In this post I'm going to show how we can achieve that and output text on our screen.I'll also share some shortcuts to make typing easier and correct any mistakes we may make along the way.
The three standard streams
When talking about standard streams in computing we are referring to the connected input and output communication channels and how they transfer data, in this case text, when a command is run. The distinction of standard has to do with where the input is being read from and where our output/error are being written to.
- standard input(stdin) is the information that we input into the terminal. That is done mostly by typing on our keyboard.
- standard output(stdout) is the information displayed after our command is run, that is in most cases our terminal screen.
- standard error(stderr) is an error message when the process has failed.
Let's take this example:
$ echo "Hello"
Hello
$
Here the echo command accepts the string "Hello" as standard input(stdin) , echoes the string "Hello" back to the terminal as standard output(stdout) and then returns another prompt($).
Echoing
The echo command is how we get the shell to print messages back to us, as it prints our text to the terminal's screen. Echo is the command and the arguments it accepts are the string of characters that we want to print.For the string of words that we use, we can either use double quotes as we did in the example above, single quotes or no quotes at all.
If we are just using a few words it doesn't really matter which one we use as long as we are consistent.
If we decide to use quotes, more often than not we may find ourselves in a sticky situation
echo "so long, farewell
>
It seems like we can't escape.
One way is to add the closing quote , or press Control+C
. We can use the later in many cases when we are in trouble in the command line.
Typing Hacks
When typing, there is a chance that we will make mistakes or that we'll want to make some changes along the way. Editing in the command line is fairly simple when we make full use of our keyboard.
- The up arrow
^
retrieves the previous command and when pressed again it moves further up the commands we have previously typed. The down arrow goes back down again. -
Control + A
get's us to the beginning of our line. -
Control + E
moves us to the end of our line. -
Control + U
deletes everything we have written so we can start fresh. - Holding down our
Option
key and clicking on what we want to change, we manage to move the cursor to the location we wish to edit. That is an effective way of editing longer sentences.
Environmental Factors
So far we have seen that the echo
command has the same role as the print function in other programming languages. Besides printing strings to the screen like we talked about earlier, we can use echo
to print the value of environmental variables to the standard output.
The environment is a working area that our shell builds every time we log in and is kept until we log out. The environment is defined by environmental variables.
One example of an environmental variable is :
$ echo $SHELL
/bin/bash
$
- This shows us the user's shell ,in this case it's
bash
. - Variables are set by using
$
as a prefix. - One thing to keep in mind with environment variables is that they are case sensitive and should have uppercase names.
This an introduction and quick overview of input/output in the command line and some basic editing that can be done. We also scratched the surface of a different topic, that of using variables in the command line which I am looking forward to learning more about in the future.
Top comments (2)
I liked this post so much again, Dionysia! And your gif game is really on point! 😄 I hope you will continue this series for a while, I'd love to learn more and you always happen to bring up such interesting information on these topics that I had no clue about! 😍 Another great read, thank you! :)
Thank you for this comment, it helps give me the motivation to keep writing them! 🤗❤️