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

前端 未结 10 1933
广开言路
广开言路 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: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
    

提交回复
热议问题