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.
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.
But there is a simpler way.
We can use awk to parse that out, without using grep at all.
This is how it works.
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.
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.
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.
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.
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.
Having the X and Y resolutions as individual variables lets you make comparisons on each dimension.
Getting hold of these values neednt be convoluted.