How to make an “alias” for a long path?

前端 未结 10 1932
广开言路
广开言路 2020-12-22 17:58

I tried to make an \"alias\" for a path that I use often while shell scripting. I tried something, but it failed:

myFold=\"~/Files/Scripts/Main\"
cd myFold
         


        
相关标签:
10条回答
  • 2020-12-22 18:31

    Another option would be to use a symbolic link. ie:

    ln -s ~/Files/Scripts/Main ~/myFold
    

    After that you can perform operations to ~/myFold, such as:

    cp some_file.txt ~/myFold
    

    which will put the file in ~/Files/Scripts/Main. You can remove the symbolic link at any time with rm ~/myFold, which will keep the original directory.

    0 讨论(0)
  • 2020-12-22 18:32

    but an actual alias for a dir is also possible, try

     myScripts="~/Files/Scripts/Main"
     alias myScripts="cd $myScripts"
    

    This way you have a common naming convention (for each dir/alias pair), and if you need to copy something from the current dir to myScripts, you don't have to think about it.

    IHTH

    0 讨论(0)
  • 2020-12-22 18:33

    There is a shell option cdable_vars:

    cdable_vars
    If this is set, an argument to the cd builtin command that is not a directory is assumed to be the name of a variable whose value is the directory to change to.

    You could add this to your .bashrc:

    shopt -s cdable_vars
    export myFold=$HOME/Files/Scripts/Main
    

    Notice that I've replaced the tilde with $HOME; quotes prevent tilde expansion and Bash would complain that there is no directory ~/Files/Scripts/Main.

    Now you can use this as follows:

    cd myFold
    

    No $ required. That's the whole point, actually – as shown in other answers, cd "$myFold" works without the shell option. cd myFold also works if the path in myFold contains spaces, no quoting required.

    This usually even works with tab autocompletion as the _cd function in bash_completion checks if cdable_vars is set – but not every implementation does it in the same manner, so you might have to source bash_completion again in your .bashrc (or edit /etc/profile to set the shell option).


    Other shells have similar options, for example Zsh (cdablevars).

    0 讨论(0)
  • 2020-12-22 18:35

    Since it's an environment variable (alias has a different definition in bash), you need to evaluate it with something like:

    cd "${myFold}"
    

    or:

    cp "${myFold}/someFile" /somewhere/else
    

    But I actually find it easier, if you just want the ease of switching into that directory, to create a real alias (in one of the bash startup files like .bashrc), so I can save keystrokes:

    alias myfold='cd ~/Files/Scripts/Main'
    

    Then you can just use (without the cd):

    myfold
    

    To get rid of the definition, you use unalias. The following transcript shows all of these in action:

    pax> cd ; pwd ; ls -ald footy
    /home/pax
    drwxr-xr-x 2 pax pax 4096 Jul 28 11:00 footy
    
    pax> footydir=/home/pax/footy ; cd "$footydir" ; pwd
    /home/pax/footy
    
    pax> cd ; pwd
    /home/pax
    
    pax> alias footy='cd /home/pax/footy' ; footy ; pwd
    /home/pax/footy
    
    pax> unalias footy ; footy
    bash: footy: command not found
    
    0 讨论(0)
提交回复
热议问题