1

I want to take a screenshot with import, and save it into a file whose name is current time.

Here is what I've tried:

sunqingyao:~$ date '+screenshot-%y%m%d-%H%M%S.png'
screenshot-170716-173336.png
# OK

sunqingyao:~$ import screenshot-170716-173336.png
sunqingyao:~$ ls -l screenshot-170716-173336.png 
-rw-rw-r-- 1 sunqingyao sunqingyao 250556 Jul 16 17:35 screenshot-170716-173336.png
# OK

sunqingyao:~$ date '+screenshot-%y%m%d-%H%M%S.png' | import
import: missing an image filename `import' @ error/import.c/ImportImageCommand/1293.
# Not OK

For some reason, I want this command to be a one-liner.

Background info:

Actually I'm trying to bind a key to screenshot taking with i3. Here is the relevant part of my ~/.config/i3/config:

# Take a screenshot upon pressing $mod+x (select an area)
# https://i3wm.org/docs/userguide.html#keybindings
bindsym --release $mod+x exec --no-startup-id date '+screenshot-%y%m%d-%H%M%S.png' | import

Seems that I can only bind a key to a command that can fit in only one line, which is why a one-liner is required.

  • 1
    Use command substitution: import $(date '+screenshot-%y%m%d-%H%M%S.png' ) and bindsym --release $mod+x exec --no-startup-id import $(date '+screenshot-%y%m%d-%H%M%S.png'). – jimmij Jul 16 '17 at 10:02
  • @SergiyKolodyazhnyy Seems that it's not done in one line in the question you linked to :/ – nalzok Jul 16 '17 at 10:06
  • 1
    Technically it is, as jimmij stated use $() , which is command substitution form – Sergiy Kolodyazhnyy Jul 16 '17 at 10:10
  • Or use pipe then xargs – Sergiy Kolodyazhnyy Jul 16 '17 at 10:10
1

You can always use command-substitution as the other answer suggests, or use xargs to use what pipe gives:

$ date '+screenshot-%y%m%d-%H%M%S.png' | xargs -I {} import  {}                                                              

$ ls screenshot-*                                                                                                            
screenshot-170716-042853.png
| improve this answer | |
1

since import will take the name of the .png as argument constract it and call the import. Think that first wil the $() will be substitute and then will be passed as argument in import

import $(date +'screenshot-%Y%m%d-%H%M%S.png')

e.g with set -xv & echo enable

$ echo $(date +'screenshot-%Y%m%d-%H%M%S.png')
echo $(date +'screenshot-%Y%m%d-%H%M%S.png')
++ date +screenshot-%Y%m%d-%H%M%S.png
+ echo screenshot-20170716-131720.png
screenshot-20170716-131720.png
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.