简体   繁体   中英

.bashrc command to copy current working directory

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM