Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • Okay this worked great and did exactly what I needed. I am trying to use this to change directory to a directory with spaces in the name just by using the arguments. What I ultimately needed this for was to create a function that will change directories without having to type the apostrophes in if the directory has words spaced out. What I just tried was the following: cdm(){ str=" '$*' " cd $str } It only accesses the first argument in str So example: cdm test 2 Result: 'test: No such file or directory Commented Apr 22, 2015 at 3:07
  • @Schwagmister: You can save it into a (string) variable if you want, but, if you don't specifically need to do that, I think cd "$*" will be good enough. Commented Apr 22, 2015 at 6:40
  • @Schwagmister The single quotes are not helpful there. Try: cdm(){ str="$*"; cd "$str"; } or, as Scott suggests, cdm(){ cd "$*"; }. Also, be aware that $* replaces multiple consecutive spaces with a single space. If your directory name may have multiple consecutive spaces or tabs or newlines, then those characters really need to be escaped before they are passed to the cdm function. Commented Apr 22, 2015 at 6:52
  • So if it is something like test 2 trial 1, how would I make this cd command work correctly? Also this is amazing help I really appreciate it guys. @Scott Commented Apr 22, 2015 at 7:08
  • @Schwagmister The best way is to put single-quotes on the command line when calling cdm as in: cdm 'test 2 trial 1'. This will handle all manor of strange names. However, as long as the white spaces are limited to single-spaces, then the cdm then it can be run as cdm test 2 trial 1. Commented Apr 22, 2015 at 7:23