Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  25] [ 0]  / answers: 1 / hits: 53878  / 2 Years ago, thu, august 11, 2022, 9:00:41

I asked here how to make cd protect spaces. Tab completion is great, but sometimes you paste the path from the clipboard so this is useful.



The solution breaks with parentheses however. No matter how you try to escape and protect the input it always returns:



bash: syntax error near unexpected token `('


Is there some way to have it also deal with parentheses? What I want is that when I enter this, it works:



cd My path with spaces (and parentheses)

More From » bash

 Answers
0

Use single quotes ' around the argument. (This works for any command, not just cd).



cd 'My path with spaces (and parentheses)'


You can't make bash pass the parentheses to cd. Getting the spaces to work at all is a very specialized hack.



Single quotes work around every character except ' itself. Replace single quotes by the 4-character sequence '''.



cd 'Apostrophe'''s a pain'


This doesn't work if the directory name begins with a hyphen, because that causes the cd command to interpret the argument as an option. Pass -- before the directory name to avoid this.



See https://unix.stackexchange.com/questions/69144/how-do-i-escape-a-sub-directory-name-with-an-ampersand-in-it/69147#69147 for more details.



Another method is to have the shell read a separate line of text instead of passing the argument on the command line.



Strapakowsky@darkstar ~$ read -r; cd -- "$REPLY"
My path with spaces (and parentheses)
Strapakowsky@darkstar ~/My path with spaces (and parentheses)$


Rather than copy-paste manually, you might as well do it programmatically. Install either xclip or xsel.



cd -- "$(xclip -o -selection clipboard)"
cd -- "$(xsel -bo)"


You might make this an alias.



alias cde='cd -- "$(xsel -bo)"'

[#31969] Saturday, August 13, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mouedi

Total Points: 420
Total Questions: 109
Total Answers: 116

Location: Philippines
Member since Wed, Aug 19, 2020
4 Years ago
;