Question

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?

Was it helpful?

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.*($\: $_

OTHER TIPS

Have you tried:

cd "`xsel`"

That should do the job, unless you have dollars($) or back-slashes (\) in your path.

If you aren't doing this programmatically, most terminals in Linux let you paste from the clipboard with a middle-click on your mouse. Of course, you'll still need to put quotes before and after your paste, like @dave suggests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top