Open In App

Shell Script to Take a Screenshot

Sometimes we need to capture the screen for later. There are various ways to perform this task: one is achieved by using the shortcut key present in the system, and the other is by using some command-line tools. Here we are going to understand those command-line tools.

Different ways to take screenshots in Ubuntu

Before starting the command line tools, we must know about the various shortcuts that can produce different screen outputs. Knowing all of them will help you to create the desired output image.



Tools to Capture Screenshots in Ubuntu

Here we have some CLI tools which can help to capture the screen. They have different variations like capturing the whole screen, capturing the active window, deciding the screenshot’s size, choosing a specific area to capture, etc.

ImageMagick

ImageMagick can read, write images in various formats like PNG, JPEG, GIF, HEIC, TIFF, DPX, EXR, Webp, Postscripts, PDF, and SVG.



Various options present in ImageMagick

Installation:

# sudo apt install imagemagick –y

Install Imagemagick

Scrot:

Some features of scrot:-

Installation:

# sudo apt install scrot

Install scrot

Gnome-screenshot:

Gnome screenshot features:-

Installation:

# sudo apt install gnome-screenshot

Install gnome-screenshot

Prepare a Shell Script to Capture a Screenshot

This shell script will take various screenshot properties and then produce the screenshot according to those constraints. Some of the conditions we are going to use are the name of the screenshot,  area of the screenshot, etc. The script will ask you to use any given tool and capture a specific region.

Open a file using gedit

# gedit screenshotApp.sh
#!/bin/bash
#This is a shell script to take Screenshots
echo -e "
[ 1 ] for Capturing the whole Screen using gnome-screenshot \n
[ 2 ] for Capturing the Active window using the Scrot \n
[ 3 ] for Capturing the Active window using the gnome-screenshot \n
[ 4 ] for Capturing the Specific Area using the Scrot \n
[ 5 ] for Capturing the Specific Area using the gnome-screenshot \n"

#Now read the user input
read UserInput

#Now we will use switch cases to implement the UserInput

case $UserInput in
    1)
    #command for Capturing the whole Screen using gnome-screenshot.
    gnome-screenshot
    # By default it will capture the whole screen
    ;;
    2)
    #command for Capturing the Active window using the Scrot.
    # syntax scrot <option> <optional filename>
    scrot -s
    # -s is to select the rectangular area or just click on the window you want to capture
    ;;
    3)
    #command for Capturing the Active window using the gnome-screenshot.
    # syntax gnome-screenshot <option>
    gnome-screenshot -w
    # here -w is for the active window
    ;;
    4)
    #command for Capturing the Specific Area using the Scrot.
    # syntax scrot <option> filename
    scrot -s myscreenshot.png
    # here -s is interactive window decision
    ;;
    5)
    #command for Capturing the Specific Area using the gnome-screenshot.
    gnome-screenshot -a
    # here -a is for specific area
    ;;
    *)
    echo -e "Please Enter Correct Input \n"
    ;;
esac

Give executable permissions

# chmod +x screenshotApp.sh

Run and give UserInput

# ./screenshotApp.sh

Output Screenshot

Whole screen output

Run again and choose a different option this time

#./screenshotApp.sh

The output screenshot

Selected area using mouse

Article Tags :