Linux Command Line Tutorial #3 - Shell features

in #utopian-io7 years ago (edited)

In this part, we'll learn about the bash shell, and a few of it's features.

What's the shell?

The shell, usually the bash shell, is the thing that you type commands into. When you talk about using Linux, what you're actually interacting with is the bash shell. The important thing to understand is that the shell is a normal application, just like any of the other applications running on a Linux system.

STDIN, STDOUT and STDERR

STDIN, STDOUT and STDERR are the three special file descriptors that are used by applications on Linux.

ELI5 file descriptor?

Basically, it's an integer that links to a file. So when you want to read/write to a file, you use the file descriptor to refer to that file.

1. STDIN

STDIN or Standard Input is used by programs to take in their input. STDIN is also represented by 0.

By default, the STDIN is the user's keyboard(because that's what you use to type in commands)

2. STDOUT

STDOUT or Standard Output is used by programs to output the result. STDOUT is also represented by 1.

By default, the STDOUT is the shell. When you run a program, the output is printed on the shell

3. STDERR

STDERR or Standard Error is used by programs to print any errors that occur while the program is running. STDERR is also represented by 2.

By default, the STDERR is the shell. When you run a program, any errors are printed on the shell.

TypeNameSymbol
STDINStandard Input0
STDOUTStandard Ouput1
STDERRStandard Error2

Input, Output and Error Redirection

1. Input Redirection

Let's say that you want your input to come from somewhere else, other than the keyboard( ie. you don't want to type it in). You can use the 0< symbol to specify a different input source. This can also be a file.

For example, the wc -l command counts the number of lines in a given piece of data. We can use the 0< to specify the file logfile.log as input for the command.

We can also use < instead of 0< for specifying input.

Output Redirection

If you want to store the output of a program instead of just displaying it on the screen, you can use the 1> symbol, or just > to specify output location. We can stream the output of ls -l to the file file7

The important thing to remember is that the > symbol overwrites the file ie. all previous contents are erased.

If you just want to append to the file, use the >> symbol instead.

As you can see, the output of the previous command has been preserved.

3. Error Redirection

If you want any errors produced by a program to go to a specific file, you can use the 2> symbol to specify the file.

As you can see, there is no file called uhdfpa . So the ls command errors out. In the second instance, we stream the errors to the file errorfile.

Using all 3 together:

All 3 types of redirection can be used together:

Pipes

Pipes are a major unique feature of UNIX and UNIX-like OSes. Pipes allow the output of a program to be fed in to another program. Pipes are represented by the | symbol.

For example, the ps aux command prints all running processes in the system, each process displayed on a new line. So, we can pipe the output of this command to wc -l to find the total number of processes running on the system:

pipes.png

Shell keyboard shortcuts

These are just a few simple keyboard shortcuts that can be used in the shell:

ShortcutDescription
CTRL+aMove the cursor to the beginning of the line
CTRL+eMove the cursor to the end of the line
CTRL+cInterrupt/close a running program
CTRL+lClear the terminal screen(you can also use the clear command to do this)
CTRL+dClose the terminal session
CTRL+rReverse history search

Using the history command to find and run previous commands

You can use the history command to get previously run commands:

If you want to rerun the command that you previously entered, you can just type !!.

Alternatively, if you want to run another previously entered command, you can type ! followed by the command number, like so:

In this screenshot, you can see that ls -l is the 500th entered command. So, if we type !500 , the command is rerun.

Sources

  1. Header image created using this image from Pixabay
  2. All screenshots are of my own system.

harshallele



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Helpful stuff! I learned some things, like the !! command :) I knew there had to more to history than I knew!

Thats the thing about Linux! You can never run out of things to learn!
Thanks for reading! :-)

Add Ctrl+r which enables finding a command in history based on its substring.

oh yeah. forgot about that. Thanks for reminding me!

Hey @harshallele I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.22
TRX 0.26
JST 0.039
BTC 95444.49
ETH 3354.43
USDT 1.00
SBD 3.15