One command to create and change directory

前端 未结 9 1090
执念已碎
执念已碎 2020-12-08 19:07

I\'m searching for just one command — nothing with && or | — that creates a directory and then immediately cha

相关标签:
9条回答
  • 2020-12-08 19:18

    Maybe you can use some shell script.

    First line in shell script will create the directory and second line will change to created directory.

    0 讨论(0)
  • 2020-12-08 19:21

    Putting the following into your .bash_profile (or equivalent) will give you a mkcd command that'll do what you need:

    # mkdir, cd into it
    mkcd () {
        mkdir -p "$*"
        cd "$*"
    }
    

    This article explains it in more detail

    0 讨论(0)
  • 2020-12-08 19:28

    For oh-my-zsh users: take 'directory_name'
    Reference: Official oh-my-zsh github wiki

    0 讨论(0)
  • 2020-12-08 19:30

    I believe you are looking for this:

    mkdir project1 && cd "$_"
    
    0 讨论(0)
  • 2020-12-08 19:30

    Maybe I'm not fully understanding the question, but

    >mkdir temp ; cd temp
    

    makes the temp directory and then changes into that directory.

    0 讨论(0)
  • 2020-12-08 19:31

    define a bash function for that purpose in your $HOME/.bashrc e.g.

     function mkdcd () {
         mkdir "$1" && cd "$1"
     }
    

    then type mkdcd foodir in your interactive shell

    So stricto sensu, what you want to achieve is impossible without a shell function containing some && (or at least a ; ) ... In other words, the purpose of the exercise was to make you understand why functions (or aliases) are useful in a shell....

    PS it should be a function, not a script (if it was a script, the cd would affect only the [sub-] shell running the script, not the interactive parent shell); it is impossible to make a single command or executable (not a shell function) which would change the directory of the invoking interactive parent shell (because each process has its own current directory, and you can only change the current directory of your own process, not of the invoking shell process).

    PPS. In Posix shells you should remove the functionkeyword, and have the first line be mkdcd() {

    0 讨论(0)
提交回复
热议问题