Bash: change into directory just created with git clone

The first action you typically take after “git clone” is to change into the newly created directory.  This can be accomplished at the Bash shell in a couple of ways.

Here is the git URL we will use as an example for this article.

# can be https or ssh, can end in .git or not
my_git_repo=https://github.com/fabianlee/blogcode.git

The easiest way is to call basename on the special underscore variable (last parameter of last command) stripping any .git suffix.

git clone $my_git_repo && cd $(basename $_ .git)

The second way requires that we specify a directory name parameter for the clone command, which creates a clean last parameter.

git clone $my_git_repo my_new_dir && cd $_

REFERENCES

tldp.org, special variables for Bash

stackoverflow.com, clone and change into directory

unix.stackexchange, cd automatically after git clone

NOTES

In a similar fashion, you can download and unarchive a file

wget -Nq https://github.com/smallstep/cli/releases/download/v0.25.0/step_linux_0.25.0_amd64.tar.gz && tar xfz $(basename $_)