1

I am trying to write an alias for the following command that takes your current working directory, surrounded by quotes, and copies it to the clipboard.

echo \"$(pwd)\" | xclip

However, writing an alias for this in .bashrc is not working quite right. This is the alias I have written:

alias cpwd="echo \"\$(pwd)\" | xclip"

But when that command is ran the double quotes are omitted. I have checked answers to similar problems, such as bash alias command with both single and double quotes, but I think I am escaping all the required characters, so I do not know why this command isn't working.

The current result of the command would be something like: home/user/My Folder rather than "home/user/My Folder" like I am wanting.

bmb
  • 361
  • 2
  • 13

1 Answers1

2

You need more escaping. You've correctly escaped the $; you need to do the same with the existing backslashes.

alias cpwd="echo \\"\$(pwd)\\" | xclip"

Alternatively, you can avoid all the escaping by using single quotes.

alias cpwd='echo \"$(pwd)\" | xclip'

Best of all, use a function instead of an alias. A function lets you write the command exactly as you would normally without any extra quotes or escaping.

cpwd() {
    echo \"$(pwd)\" | xclip
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578