How to find width and height of the terminal in linux

You may wonder how do I find the number of characters will fit in a line in the terminal or size of the window. There are several ways to figure out the size of the window. Let’s go through the different ways to find size of terminal

First lets see different ways that you can use to figure out width and height of terminal in terms of characters

  1. Using command tput
  2. Using command stty
  3. Using environment variables

    Using command tput

The command tput is very helpful to query the terminal information and to do some other simple operations like placing cursor at required random position.

Here is how you can find the width and height of terminal. You may call it as number of rows and columns

Using command stty

This is also one of the command which useful to query,change and print terminal line settings.

The simple output of stty command regarding information of terminal

Command to get lines and columns count in characters

ūüôā

Using environment variables

On you terminal always you can query the size of the terminal using the environment variables $COLUMNS

Values of those environment variables will change if size of the terminal window changes.

How does bash fork bomb :(){ :|:& };: work

The fork bomb is kind of DOS (denial-of-service) attack on system. It attacks the system by consuming all resources. It makes use of the fork operation, that is why it is called as fork bomb.

Here is the bash version of fork bomb

:(){ :|:& };:

It is nothing more than bash function definition and calling it recursively to consume all system resources. We can also write this fork bomb in different programming languages like python. It creates endless number of processes to consumes all system resources.

WARNING! ¬† These examples may crash your system. Please don’t try it on production machines or in your libraries or on your machine. If you want to test. Try it executing in Virtual Machine¬†

….

Once the fork bomb is successfully executed on system it may not be possible to bring system back into normal state with out restarting it.

Break down of bash fork bomb :(){ :|:& };:

Bash fork bomb

:(){ :|:& };:

Here is elaborated  version of bash fork bomb,

Where,

 :()  Р is formal bash function definition. That is, we created a function named as : (colon).  We are calling the same function recursively twice in side function definition.

:|:   Р This function is getting called itself and piping output to another function call using |.  This is programming technique recursion nothing but a process of calling function itself.

& Р  This thing will put the process execution to background

;   Р  Termination of function definition

: ¬† – ¬† ¬†Calling the defined function (:). It initiates the fork bomb. With out this thing fork bomb won’t get started.

Here is human readable version of bash fork bomb. Probably it might make sense,

fbomb(){ fbomb | fbomb &}; fbomb

That is,

This fork bomb is some times used by system administrators to test process limits of user. You can save the system from crashing by setting process limits. These process limits can be changed using command ulimit  or these limits can configured  at /etc/security/limits.conf and PAM.  Process limits can be configured per user also.
If you set process limits to any process, all child processes will inherit those limits.

Well configured system would not go down when fork  bomb is initiated.

To query all existing limit try the following command.

ulimit -a

You can try executing fork bomb by reducing these limits. So, once these limits are hit, kernel will impose a restriction over creating new processes, this is how you can protect the system from crashing  because these kind DOS attacks.

You can run the fork bomb by reducing max user process limits

ulimit -u 100

….

As there is restriction over creating endless processes your system won’t go down or no need to restart to resume normal operation.