Substitution of a command, but without interrupting the output in multiple arguments

Is there a way to perform command substitution in the BASH shell without breaking the output on multiple arguments?

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

cd `xsel`

      

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

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

+2


source to share


3 answers


cd "$(xsel)"

      

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



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

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

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

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

      

+5


source


You tried:

cd "`xsel`"

      



This should do the job if you don't have dollars ($) or backslashes (\) in your path.

+2


source


Unless you do it programmatically, most terminals in Linux allow you to paste from the clipboard with a middle click. Of course, you will still have to put quotes before and after the paste as @dave suggests.

0


source







All Articles