5 ms·
This function will fail when given an argument with a space, which would be quite frustrating for a beginner, which is presumably the target audience for such a
by dundarious 6y ago
This function will fail when given an argument with a space, which would be quite frustrating for a beginner, which is presumably the target audience for such a document:
> mcd() { mkdir -p $1; cd $1 }
It should be more like:
> mcd() { mkdir -p "$1"; cd "$1"; }
The trailing ";" is also required in bash for oneline blocks like this.
Same goes for most of the other functions on page 70. "$@", etc.
Just trying to make the learning experience less frustrating, not trying to tear down the effort :)
- ketanmaheshwari 6y agoAh thank you! I will fix it.
- deleted 6y ago[deleted]
- sandreas 6y agofunction mkcd() { mkdir -p "$@" && cd "$1"; } supports multiple dirs and cd to the first one mkcd a b c
- enriquto 6y agoa beginner would not likely quote the argument, so the point is moot... besides, what kind of savage wants a directory name with spaces?
- hpcjoe 6y ago(looks at all his directories which are written as phrases and ...) [raises hand] This type of savage?
- dundarious 6y agoTrue, they'd have to learn how to use arguments with spaces in the shell -- but this won't necessarily happen near the time they add mcd to their .bashrc, which may lead to frustration with customization, and stifle the desire to learn. Something to be avoided when it is simple and easy to do so.
- yjftsjthsd-h 6y ago> a beginner would not likely quote the argument, so the point is moot... But a beginner could still tab-complete file names and get them automatically backslashed.
- deleted 6y ago[deleted]
- je42 6y agoit probably makes sense to use "&&" instead of ";": if mkdir -p fails cd is still being executed.
- pjungwir 6y agoIf we're being picky then you probably also want `mkdir -p -- "$1"` in case they want to create a directory named `-m`, `-v`, etc. Surprisingly `--` isn't mentioned in the mkdir manpage on my system, but it works. E.g. `mkdir -- -p` does what you'd want.
- a1369209993 6y agoStrictly speaking you want `mkdir -p "./$1"`, although the failure modes of `--`-ing arguments aren't as immediately demonstratable as those of not deflagging them at all.
- dundarious 6y agoYou definitely don't want to universally add the "./" prefix because it breaks on absolute paths. The only solution is for users to add the "./" prefix themselves when the path begins with "-", because many BSD userspaces don't support "--" as a marker to stop option parsing. Maybe that doesn't matter for a document with Linux in the title, but given the popularity of macOS for doing Linux development, I would still err on the side of supporting the BSDs for such things. Never mind that support for "--" outside GNU is very spotty, even on Linux. There is no point, in my opinion, in trying to be smart in small shell helper functions. It should just do a simple, relatively foolproof series of commands, and usually, bail early on error. But the intersection of `mkdir -p` failures and `cd` successes is tiny, so I didn't even bother muddying the waters by suggesting another alteration to use `&&`.
- fiddlerwoaroof 6y agoI still do this reflexively but zsh doesn’t word-split variable expansions, so the first version would work as expected. Little things like this have made me prefer using zsh whenever feasible: and, in the age of dockerized everything, there’s few good reasons left to use bash.