Quick Links
Summary
If you need to know your current screen resolution on a Linux computer, you have several commands to choose from. Sometimes it is useful to incorporate that functionality in a Bash script.
Why Change Your Screen Resolution
Sometimes you need to know what your screen resolution is before your script launches an application or does some further processing. You may need to pass the resolution as command line parameters to the application you’re about to launch, or you may have reset the resolution and you want to check that the change of resolution was successful.
Lowering the resolution is a trick that can make playing some games on older, less powerful, hardware possible. Changing the resolution back and forth manually will soon get tiresome, so the obvious answer is to do it from within a script.

The commands we’re going to look at were installed as standard on all the distributions we checked, so there shouldn’t be a need to install anything. You can 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. By default, with no parameters, it generates a list of video modes that are supported by your screen, or screens.
The preferred video modes are followed by a plus sign “+”, and the current video mode is followed by an asterisk “*”.

This screen has only one preferred video mode, and it happens to be the current video mode. Although you can have several preferred modes, there can only be one current mode. That means if we can find the line with the asterisk in it, we can determine what the current video mode settings are.
You might see online examples where this is what they do. They use grep to find the line with the asterisk in it, and then use sed or awk to parse out the information we’re interested.

But there is a simpler way. The information in the output from xrandr contains details of the “connected primary” screen, including the current video mode resolution. We can use awk to parse that out, without using grep at all.
This is how it works. We use the -F (field separator) option to tell awk that spaces and plus signs “+” are to be treated as field delimiters. We’re searching for lines with the word “primary” in them. This word was picked because it appears in the line we’re interested in, and doesn’t appear anywhere else in the output from xrandr.
Splitting that line into fields at spaces and plus signs “+” means the current resolution is located in field four.
We use the awk print command to print the fourth field. Because we’re also splitting the fields at plus signs, the “+0+0” is trimmed off the back of the resolution, becoming fields five and six. We’re not interested in those.
Related:How to Use the awk Command on Linux
Using the xdpyinfo Command
By default, the xdpyinfo command displays a lot of information about your screen and available video modes. 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.” That word doesn’t appear anywhere else in the output, so we can use it to isolate the line of interest, using awk.
It’s a simpler command than the one we used with xrandr, because we don’t need to specify any field delimiters. awk defaults to using whitespace as field delimiters, and that’s all that we have to contend with in the output from xdpyinfo. The resolution is the second field in the line of output, and {print $2} writes that to the terminal window for us.
Putting It Into a Script
Using either of these techniques in a script requires very little extra effort. We need to have a variable to hold our discovered resolution so that we can make a judgment in our script about whether it ought to carry on processing or exit if the current screen resolution doesn’t meet its particular needs.
Copy these lines to an editor, save the file as “get_res.sh”, then close your editor.
We’ll need to make the script executable using the chmod command.
This assigns the output of the awk command to a variable called current_res, and echoes the value of that variable to the terminal window. Let’s run the script.
Encouragingly, we see the same results as we did when running the commands in the terminal window manually.
Separating the X and Y Resolutions
If you want to separate out the X and Y resolutions, we can add a couple of lines to do just that. Copy these lines to your editor, save the file as get_x_y.sh, and close your editor.
We can use awk to extract the X resolution from the value stored in the current_res variable, and store the result in a variable called current_x. 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.
Here’s 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. Perhaps it is only the horizontal resolution that matters to you, and as long as it meets or exceeds a minimum, your script can proceed.
Keep It Sweet and Simple
Being able to isolate the vertical and horizontal resolutions lets you test either one of them, or both of them, to make sure your resolution-sensitive script can continue. It also lets you use the values as command line parameters for other applications that need to be launched with that information.
Getting hold of these values needn’t be convoluted. Even a little knowledge of the standard Linux utilities such as awk will repay the time spent learning them, many times over.