2021-01-21

Linux screen resolution script

linux, bash, script, screen

banner

Image by Pexels from Pixabay

TL;DR

Change Ubuntu desktop resolution using xandr.

  1. Create a script to change resolution
1#!/bin/bash2xrandr --output eDP-1-1 --mode $(basename $0)
  1. Create "hard" symlinks with resolution as file names
1# Make `resolution.sh` executable2# Create hard symlinks on the desktop3chmod +x ./resolution.sh4ln ./resolution.sh ~/Desktop/1920x10805ln ./resolution.sh ~/Desktop/1280x720

Introduction

During the pandemic, I started playing CS:GO (Counter Strike: Global Offensive) a lot.

When the desktop resolution doesn't match the game resolution, the game looks blurry.
I play at 720p while the desktop is 1080p.

Changing resolution via Nautilus has its annoyance because it displays so many resolution options.

So I decided to write a bash script to change the resolution
with a click of a button.

Scripts

Initially I created two scripts

File Name: 1280x720

1#!/bin/bash2xrandr --output eDP-1-1 --mode 1280x720

File Name: 1920x1080

1#!/bin/bash2xrandr --output eDP-1-1 --mode 1920x1080

It worked great.
But in Coding Blocks slack channel (#Linux), Dave Follett gave me a tip that I can "abstract" the resolution by reading the file name as the resolution.

And why not?

Abstracting the script

Dave taught me that $0 expands the name of the shell/script and to extract the resolution (file name), I could use basename.

So basically for a file on Desktop, ~/Desktop/1280x720, $0 returns /home/dance2die/Desktop/1280x720 while basename $0 will remove the directory, returning 1280x720.

Then the result 1280x720 is passed to xrandr.

Implementation

This means, I need to create 3 files; One main script, two hard symlinks.

Mainscript: ~/scripts/resolution.sh

1#!/bin/bash2xrandr --output eDP-1-1 --mode $(basename $0)

And then create two hard symlinks.

1# Make `resolution.sh` executable2chmod +x ./resolution.sh3# Create hard symlinks on the desktop4ln ./resolution.sh ~/Desktop/1920x10805ln ./resolution.sh ~/Desktop/1280x720

Image by Pexels from Pixabay