Xfce Forum

Sub domains
 

You are not logged in.

#1 2021-07-03 11:44:00

D.dave
Member
Registered: 2019-12-06
Posts: 35

[SOLVED] Bash script to copy file path to clipboard doesn't work

Hi, I have the following bash script, which I use to get the files path and have such path surrounded in quotes (which I need, eg, when I copy filenames which contains spaces (I inserted it in Custom Action of Thunar)

Name of the script: copyfilepath

#!/bin/bash

filepath="$@"
echo -n $filepath | sed -e "s/,/\',\'/g" -e "s/^/\'/" -e "s/$/\'/" | xclip -selection clipboard

Example:
On the Desktop I have this text file: /home/dave/Desktop/test file
In the Terminal I execute:

copyfilepath /home/dave/Desktop/test file

And as I press CTR+V, I got '/home/dave/Desktop/test file' as expected (inside quotes)
I inserted such script in a custom action inside Thunar but I don't get anything in the clipboard. Why?
Should I change something or someone can suggests me a different solution? What is important is to have the file path surrounded with single quotes.

Last edited by D.dave (2021-07-03 12:27:37)

Offline

#2 2021-07-03 12:07:34

D.dave
Member
Registered: 2019-12-06
Posts: 35

Re: [SOLVED] Bash script to copy file path to clipboard doesn't work

If, as custom action in Thunar, I insert

echo -n %f | sed -e "s/,/\',\'/g" -e "s/^/\'/" -e "s/$/\'/" | xclip -selection clipboard

This command works as expected, but I would prefer to use the script.

Offline

#3 2021-07-03 12:26:15

D.dave
Member
Registered: 2019-12-06
Posts: 35

Re: [SOLVED] Bash script to copy file path to clipboard doesn't work

Sorry for the noise!
Seems that I had to edit the bash scipt as following:

echo -n "$@" | sed -e "s/,/\',\'/g" -e "s/^/\'/" -e "s/$/\'/" | xclip -selection clipboard

And now is working from the custom action:

~/scripts/thunarscripts/copyfilepath %f

Offline

#4 2021-07-03 21:09:20

Tamaranch
Member
Registered: 2020-12-31
Posts: 18

Re: [SOLVED] Bash script to copy file path to clipboard doesn't work

Your script could use only builtin bash smile

#!/bin/bash

printf '%s\n' "${@@Q}" | xclip -selection clipboard

Then

~/scripts/thunarscripts/copyfilepath %F

You can replace \n with whatever you want in printf '%s\n'.

Offline

#5 2021-07-05 13:37:23

PaperNick
Member
From: Bulgaria
Registered: 2013-05-26
Posts: 89

Re: [SOLVED] Bash script to copy file path to clipboard doesn't work

Hey Tamaranch, can you explain what the following snippet does?

"${@@Q}"

I know about $@ but I've never seen this. Curious to know smile

Offline

#6 2021-07-05 15:41:04

Tamaranch
Member
Registered: 2020-12-31
Posts: 18

Re: [SOLVED] Bash script to copy file path to clipboard doesn't work

This is a special case of parameter expansion, named "parameter transformation" in man bash:

man bash wrote:

${parameter@operator}
The expansion is either a transformation of the value of parameter or information about parameter itself, depending on the value of operator.
Each operator is a single letter:
[…]
Q   The expansion is a string that is the value of parameter quoted in a format that can be reused as input.
[…]

As usual, in case of an array (which is what the first @ in "${@@Q}" is), expansion applies to each element:

$ bash -c 'printf "%s\n" "${@@Q}"' _ a b c
'a'
'b'
'c'
$

Offline

#7 2021-07-05 19:17:05

PaperNick
Member
From: Bulgaria
Registered: 2013-05-26
Posts: 89

Re: [SOLVED] Bash script to copy file path to clipboard doesn't work

I get it now, thanks! This can be quite useful.
Grepping the man bash page for @@ or @Q didn't yield any results, so thanks for the explanation.

Offline

Board footer

Powered by FluxBB