Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 702  / 3 Years ago, fri, september 24, 2021, 10:30:11

How can I enable bash expansion by custom keyboard shortcuts in 14.04? Here's an example that I've been trying to get setup; I want to execute this exact command (which works from a terminal running bash):



gnome-screenshot --window --remove-border -f ~/Pictures/screenshot_$(date +%x_%H:%M:%S:%N).png


It doesn't work as is, and neither does:



gnome-screenshot --window --remove-border -f $HOME/Pictures/screenshot_$(date +%x_%H:%M:%S:%N).png


If I change it to the following:



gnome-screenshot --window --remove-border -f "/home/user/Pictures/screenshot_$(date +%x_%H:%M:%S:%N).png"


I end up with a file on my desktop called "screenshot_$(date +%x_%H:%M:%S:%N).png" (no subshell expansion). What am I doing wrong? What executes these commands if not bash? Why aren't the subshells or variables/tildes expanded?


More From » gnome

 Answers
7

The keyboard shortcuts field in Unity works in a similar manner to .desktop files, i.e. commands are executed without any form of subshell or variable expansion and can't access any environment variables.



I wasn't able to find an official specification regarding the keyboard shortcuts menu but I think it's save to assume that command execution follows similar guidelines to the Exec field in XDG .desktop files.



What this means, in practice, is that there are two ways you can make your custom command-line work with the shortcuts menu:




  1. Transform your commands into a simple shell script and point the keyboard shortcut to the shell script.



    For instance, you could paste the following into a new text file, name it something like custom_screenshot_script and make the script executable:



    #!/bin/bash
    gnome-screenshot --window --remove-border -f "~/Pictures/screenshot_$(date +%x_%H:%M:%S:%N).png"

  2. Call bash to execute your command.



    This is more tricky because you have to escape your arguments correctly. Something like the following should work:



    bash -c "gnome-screenshot --window --remove-border -f "~/Pictures/screenshot_$(date +%x_%H:%M:%S:%N).png""


[#23766] Saturday, September 25, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bathtusain

Total Points: 380
Total Questions: 124
Total Answers: 111

Location: Trinidad and Tobago
Member since Sat, Apr 9, 2022
2 Years ago
bathtusain questions
Sun, Jul 17, 22, 03:13, 2 Years ago
Sun, Oct 3, 21, 00:24, 3 Years ago
Sat, Apr 22, 23, 00:24, 1 Year ago
Fri, Jul 22, 22, 12:01, 2 Years ago
Sun, Jun 27, 21, 02:31, 3 Years ago
;