How to make an "alias" for a long way?

I tried to create an โ€œaliasโ€ for the path that I often use when working with the shell. I tried something, but it failed:

myFold="~/Files/Scripts/Main" cd myFold bash: cd: myFold: No such file or directory 

How do I make it work?
However, cd ~/Files/Scripts/Main does work.

+78
linux scripting unix bash
Jul 30 '13 at 22:28
source share
9 answers

Since the environment variable (the alias has a different definition in bash ), you need to evaluate it with the following:

 cd "${myFold}" 

or

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

But I find it easier if you just want to easily switch to this directory to create a real alias (in one of the bash startup files, such as .bashrc ), so I can save keystrokes:

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

Then you can just use (without cd ):

 myfold 

To get rid of the definition, you use unalias . The following transcription shows it all 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 
+102
Jul 30 '13 at 22:37
source share

There is a cdable_vars shell cdable_vars :

cdable_vars
If this is set, the argument to the cd built-in cd which is not a directory, is assumed to be the name of a variable whose value is the directory to be changed.

You can add this to your .bashrc :

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

Note that I replaced the tilde with $HOME ; quotation marks prevent tilde expansion, and Bash will complain about the lack of the ~/Files/Scripts/Main directory.

Now you can use it as follows:

 cd myFold 

$ Not required. The fact of the matter is that in fact - as shown in other answers, cd "$myFold" works without a shell option. cd myFold also works, if the path in myFold contains spaces, myFold not required.

This usually even works with autocomplete tabs, since the _cd function in bash_completion checks if cdable_vars is cdable_vars - but not every implementation does the same, so you may bash_completion again bash_completion in your .bashrc (or edit /etc/profile to set the option shell).




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

+50
Oct. 03 '16 at 19:51
source share

Maybe it's better to use links

Soft link

A symbolic or programmatic link (files or directories that are more flexible and self-documenting)

 # Source Link ln -s /home/jake/doc/test/2000/something /home/jake/xxx 

Hard link

Hard link (only files, less flexible and not self-documenting)

 # Source Link ln /home/jake/doc/test/2000/something /home/jake/xxx 

How to create a directory link

Hint : if you do not need to see the link in your home, you can start it from a point. ; then it will be hidden by default, then you can access it as

 cd ~/.myHiddelLongDirLink 
+8
Nov 06 '18 at 21:33
source share

You can add any paths to the hash table of your bash:

hash -d <CustomName>=<RealPath>

Now you can cd ~<CustomName> . To make it persistent, add it to your bashrc script.

Please note that this hash table is designed to provide a cache for bash, it does not need to search for content every time the command is executed, therefore this table will be cleared for events that are not valid for the cache, for example. change $PATH .

+6
Oct 03 '16 at 19:23
source share

First you need $ to access the value "myFold" for the code to work in the question:

 cd "$myFold" 



To simplify this, you create an alias in ~/.bashrc :

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

Remember to specify .bashrc once to make the alias available in the current bash session:

 source ~/.bashrc 

Now you can go to the folder using:

 cdmain 
+5
Jul 30 '13 at 22:30
source share

First of all, you need to remove the quotes:

 bashboy@host:~$ myFolder=~/Files/Scripts/Main 

Quotation marks prevent the shell from expanding the tilde to its special meaning as your $HOME directory.

Then you can use $myFolder for the environment variable:

 bashboy@host:~$ cd $myFolder bashboy@host:~/Files/Scripts/Main$ 

To create an alias, you need to define an alias:

 alias myfolder="cd $myFolder" 

Then you can consider this kind of command:

 bashboy@host:~$ myFolder bashboy@host:~/Files/Scripts/Main$ 
+5
Jul 31 '13 at 3:14
source share

Another option is to use a symbolic link. i.e:

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

After that, you can perform operations with ~/myFold , for example:

 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 save the source directory.

+4
Dec 05 '17 at 21:42 on
source share

but a real alias for the directory is also possible, try

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

So you have a general naming convention (for every dir / alias pair), and if you need to copy something from the current directory to myScripts, you don't need to think about it.

Ihth

+2
Jul 30 '13 at 22:36
source share

Put the following line in your myscript

 set myFold = '~/Files/Scripts/Main' 

In the terminal use

 source myscript cd $myFold 
-one
Jun 11 '19 at 9:23
source share



All Articles