R – command substitution but without breaking output into multiple arguments


Is there a way to do command substitution in BASH shell without breaking output into multiple arguments?

I copy the path of some directory (from the location bar in a GUI file browser) to clipboard and then issue the following command, where the command xsel returns the clipboard content, which is the path of the directory in this case:

cd `xsel`

But some path contain spaces or may even contain some special characters used by BASH.

How can I pass the output of a command as a single argument and without BASH messing with special characters?

Best Solution

cd "$(xsel)"

seems to handle all special characters (including $ and spaces).

My test string was boo*;cd.*($\: $_

$ mkdir "$(xsel)"
$ ls
boo*;cd.*($\: $_

$ file boo\*\;cd.\*\(\$\\\:\ \$_/
boo*;cd.*($\: $_/: directory

$ cd "$(xsel)"
$ pwd
/tmp/boo*;cd.*($\: $_