cd folder &>/dev/null ||( mkdir folder && cd folder ) && git clone https://github.com/user/my-project.git
when running the above command I have the problem (I think), that the ()
spawns a subshell and runs the commands, then when running the git clone I'm not in the expected directory. Is there a way to run this line? My current solution is
cd folder &>/dev/null || mkdir folder && cd folder && git clone https://github.com/user/my-project.git
The thing is, I run cd folder
twice even if the directory exists
There are two grouping operators in shell:
( commands… )
Runs commands in a subshell{ commands… }
Runs commands in the current execution environment.But please be aware that ( and ) are shell meta-characters while { and } are not. The braces are reserved words, but only if they are complete words and appear as the first word in a command. So the braces need to be surrounded by spaces and the closing brace must come after a semi-colon. For a longer explanation, see Bash command groups: Why do curly braces require a semicolon?.
Specifically, you would have to write
cd folder &>/dev/null || { mkdir folder && cd folder; } && git clone https://github.com/user/my-project.git
Note the explicit semicolon to terminate the pipeline. You could have used it in the subshell compound command, too, but here you must use it.
However, you shouldn't really use this command, because it could fail if another process happened to create the directory folder
after the first cd
fails and before the mkdir
runs. There is a better way:
Because this is a very common task indeed, mkdir
comes with the useful -p
option (this option is required by Posix).
mkdir -p some/path
does two things differently:
The intermediate directories are also created, if necessary
No error is produced if the final directory already exists.
So the common idiom for tasks like this is:
mkdir -p folder && cd folder && git clone https://github.com/user/my-project.git
which would work even if folder
were a complete path and more than one directory along the path needed to be added.
The () does spawn a sub-shell. Here's an example of a one-line command that might be helpful:
if [ ! -d folder ]; then mkdir folder; fi; cd folder && git clone ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With