Sometimes it is useful to incorporate that functionality in a Bash script.

you could use these techniques straightaway, and your scripts should be portable across different distributions.

Using the xrandr Command

The xrandr command can be used to set and query the screen resolution.

Terminal window showing the manual page for the Mutt email client on Linux

This screen has only one preferred video mode, and it happens to be the current video mode.

Although it’s possible for you to have several preferred modes, there can only be one current mode.

You might see online examples where this is what they do.

Four hard drivers sticking out of a rack-mount server that’s being used as a NAS.

But there is a simpler way.

We can use awk to parse that out, without using grep at all.

This is how it works.

The Terminal open on Linux.

Hannah Stryker / How-To Geek

Were searching for lines with the word primary in them.

We use the awk print command to print the fourth field.

We’re not interested in those.

The default output from the Linux xrandr command

It generates well over 1000 lines of output on our test machine.

The current mode is described in more detail than the other available modes.

The line describing the resolution of the current mode includes the word dimensions.

Using awk with xrandr to isolate the current screen resolution

Copy these lines to an editor, save the file as get_res.sh, then close your editor.

Well need to make the script executable using the chmod command.

Let’s execute the script.

The default output from the xdpyinfo command

Encouragingly, we see the same results as we did when running the commands in the terminal window manually.

Copy these lines to your editor, save the file as get_x_y.sh, and close your editor.

We repeat the process to extract the Y resolution, and assign it to a variable called current_y.

Piping xdpyinfo through awk to isolate the current screen resolution

Finally, we print all three variables.

Again, we need to use chmod to make it executable.

Heres the output from the script on our test machine.

Making the get_res.sh script executable

Having the X and Y resolutions as individual variables lets you make comparisons on each dimension.

Getting hold of these values neednt be convoluted.

Running the get_res.sh script

Using chmod to make teh get_x_y.sh script executable

Running the get_x_y.sh script