2
\$\begingroup\$

Some archives contain folders with spaces in their name. Sometimes while I'm browsing these folders, I want to open a terminal in current position, so I copy the path to paste it after a cd command. Of course when there are spaces in the path, cd won't work unless all spaces are escaped.

Since I find it annoying not to be able to paste paths after cd, I just added this function to my .bash_aliases:

function cd {
  if [ -z "$*" ]; then
    command cd
  else
    command cd "$*"
  fi
}

So mkdir "an awful dir name" && cd an awful dir name now works as expected (what else should cd an awful dir name do?).

Since .bash_aliases shouldn't affect non-interactive shells, other scripts should still work with builtin cd.

Now the question is: can this trick cause problems on my shell? Or am I freaking out for no reason?

\$\endgroup\$
  • \$\begingroup\$ "Now the question is: can this trick cause problems on my shell?" Your shell usually uses spaces as separator. The moment you start chaining functions, things might no longer work as expected. \$\endgroup\$ – Mast Jun 27 '17 at 14:04
  • \$\begingroup\$ Ask yourself: will this work with two spaces? With space and tab? Or with * and &? And given that your function is specific to cd, it's probably less effort in the long run just to put single-quotes around what you're pasting than to try to 'unexpand' the command. (Hmm, Markdown shows those two spaces as one, and the tab as a single space. But I think you can see the problem). \$\endgroup\$ – Toby Speight Jun 27 '17 at 14:25
  • \$\begingroup\$ @TobySpeight yep, I see the problem here! Anyway I'm breaking option parameters like -P... That's why I asked : ) \$\endgroup\$ – emiliano.bovetti Jun 27 '17 at 14:34
  • \$\begingroup\$ There's no need to add the solution to the question. Accepting the answer was sufficient. \$\endgroup\$ – Jamal Jun 27 '17 at 22:58
3
\$\begingroup\$

Throw away your current code. Instead write this:

for dir in *; do
  (cd "./$dir" && echo "In $(exec pwd) now.")
done

As a general rule, always put variables into quotes.

Rationale: Your current code can handle a few cases properly, but some other special characters will still be interpreted specially by the shell and lead to unexpected behavior. Therefore the proper way to work around this problem is to write cd ' into the terminal, then to paste the path and then to finish the command by typing '. This will work for all characters except single quotes.

Alternatively, you could invent a command cdclip that changes to the path in the clipboard. Cygwin has /dev/clipboard, and on native Linux there is probably some similar way to read the clipboard contents.

\$\endgroup\$
  • \$\begingroup\$ cdclip is indeed a better idea. cd "$(xclip -o -selection primary)" seems to work just fine on Linux. Thanks \$\endgroup\$ – emiliano.bovetti Jun 27 '17 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.