You are not logged in.
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
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
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
Your script could use only builtin bash
#!/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
Hey Tamaranch, can you explain what the following snippet does?
"${@@Q}"
I know about $@ but I've never seen this. Curious to know
Offline
This is a special case of parameter expansion, named "parameter transformation" in man bash:
${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
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
[ Generated in 0.009 seconds, 7 queries executed - Memory usage: 539.63 KiB (Peak: 540.55 KiB) ]