[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: realpath quoting



On 03/05/2024 11:31, jeremy ardley wrote:
My use case is very simple. Give an argument to a program that expects a single filename/path.

Role of realpath in your workflow is not clear for me yet.

If you need to copy its result to clipboard then you may use xsel, xclip, etc.

realpath --zero "$file" |
    { IFS= read -r -d '' path ; printf '%q' "$path" ; } | xsel -bi

You may bind some key sequence to paste PRIMARY or CLIPBOARD content to BASH prompt quoted

_bind_x_yank() {
    local buffer head tail
    if [ -z "$READLINE_ARGUMENT" ]
    then
        buffer="$(xsel --output "${1:---primary}")"
    else
        buffer="$(xsel --output "${1:---primary}" |
            xargs --null printf '%q')"
    fi
    [ -n "$buffer" ] || return
    head="${READLINE_LINE:0:$READLINE_POINT}${buffer}"
    tail="${READLINE_LINE:$READLINE_POINT}"
    READLINE_LINE="${head}${tail}"
    READLINE_POINT="${#head}"
}
bind -x emacs -x '"\C-xY": _bind_x_yank'
bind -x emacs -x '"\C-xy": _bind_x_yank --clipboard'

[Esc] [1] [Ctrl+x] [y]
from clipboard or last [Y] from PRIMARY selection.

You even may define a desktop-wide shortcut that replaces selection content with its quoted variant. Neither task requires quoted output from realpath directly.

I am unsure what kind of debugger you use and what kind of escaping it needs.

P.S.
A corner case is a file path having trailing newlines
https://mywiki.wooledge.org/BashPitfalls#content.3D.24.28.3Cfile.29


Reply to: