4

Can bash aliases be paths?

Hi and sorry in advance for the noob question. I noticed a strange behavior when using bash aliases.

My goal

is to use short bash aliases for often used paths, to make navigation in the shell easier. As an esample, to quickly navigate to a university (uni) folder on a data drive mounted at /mnt/data I used the bash alias alias uni='/mnt/data/uni'. Now, if I am in the parent directory /mnt/data and run cd uni, this works as I though it would and I move to /mnt/data/uni.

The issue

However if I am on another drive, say in my root directory and I run the same, I get the error bash: cd: uni: No such file or directory. If I only execute uni, the output is bash: /mnt/data/uni: Is a directory indicating that bash "understands" that this is a directory. However, for some reason, when not in the parent directory, the alias can not be used with the cd function to move to the directory.

If I add the cd command to my alias so that it reads alias uni='cd /mnt/data/uni', the alias works from all directories. However I would like to have only the path in the alias to be able to use other functions with it. Now I could live with including the cd into the alias because 99% of operations I do with these directories is just navigation but what bugs me most is that I obviously don't fully understand how aliases work in this situation.

My question

Could somebody explain why my alias behaves this way and what I am missing here? A solution to only alias the path would also be handy.

Thanks a lot! Cheers

8
  • What are the other functions you would want to use with the pathname? Have you considered putting the pathname in an ordinary variable (unipath=/mnt/data/uni) and then using something like alias uni='cd "$unipath"'? That would give you an alias for the cd command with your path, as well as a variable that you could use with other functions. I'm not turning this into an answer as you have not said anything about what other things you need to do that stop you from using an alias the way one usually does.
    – Kusalananda
    Commented Mar 13, 2022 at 10:15
  • Relating, though not an exact answer: unix.stackexchange.com/q/25327/315749
    – fra-san
    Commented Mar 13, 2022 at 10:16
  • @they other functions would be to path the path to an rsync command to eventually backup my stuff to an external drive (once I have time to write up a script to leave the gui application I currently use). You are completely right, using a variable is probably the sane way to do this, which I haven't thought about yet since this is really the first time I mess with my bash configuration. Can variables also live in the .bashrc_aliases file?
    – weygoldt
    Commented Mar 13, 2022 at 10:23
  • Possible duplicate - Quick directory navigation in the bash shell Commented Mar 13, 2022 at 10:43
  • @animbehav I suspect you mean ~/.bash_aliases file. Yes, that can work, but using ~/.bashrc is the standard. The ~/.bash_aliases file is an Ubuntu addition which is explicitly sourced by the ~/.bashrc file, but the normal config file is ~/bashrc.
    – terdon
    Commented Mar 13, 2022 at 11:47

3 Answers 3

5

No, aliases can't be paths. Or, more precisely, they can only be paths to something that can be executed. Aliases are run as commands, so alias uni=/bin/ls' will work because this is a path that points to a command. However, alias uni=/mnt/data/uni will not work because uni is a directory and that can't be executed.

The reason it works when you are in /mnt/data is because you have a subdirectory there named uni, so cd uni simply moves into that directory. The alias isn't even being invoked here. It isn't relevant at all. All the shell sees is a command (cd) and a target (the directory uni), the alias functionality isn't being called at all.

If you want to have directories easily accessible like that, you can set them up as variables. Add this to your ~/.bashrc file:

uni="/mnt/data/uni"

Save the file, open a new terminal, and you can now do cd "$uni" to move to /mnt/data/uni from anywhere.

2
  • Okay that clarifies the use of variables and aliases, thanks a lot. After reading the explanation why the "alias" works in the parent directory makes me feel very stupid now.
    – weygoldt
    Commented Mar 13, 2022 at 12:44
  • @animbehav never confuse ignorance and stupidity! Ignorance is easily fixed :) We all had to learn these things at some point.
    – terdon
    Commented Mar 13, 2022 at 13:13
3

Aliases can't be paths. They can only be command. If you run cat uni/file.txt or even cd uni, bash never tries to expand uni as an alias. If you want this to happen, the closest thing would be to make it a variable.

uni=/mnt/data/uni
cd $uni
cat $uni/file.txt

Note that if the path contain spaces (or other special characters rarely found in file names), you need double quotes when using the variable:

pol="/mnt/data/Pol Sci"
cat "$pol/file.txt"              # works
cat "$pol"/file.txt              # works
cat $pol/file.txt   # doesn't work: tries to print the content of /mnt/data/Pol and of Sci.txt

If you enable the cdable_vars option, you can save typing the $ and double quotes.

shopt -s cdable_vars
pol="/mnt/data/Pol Sci"
cd "$pol"                        # works
cd $pol                          # doesn't work ("cd: too many arguments")
cd pol                           # works thanks to cdable_vars

If you're only interested in a shortcut for the cd command, however, you can simply include cd in the alias.

alias uni='cd /mnt/data/uni'
uni                              # equivalent to cd /mnt/data/uni
alias pol='cd "/mnt/data/Pol Sci"'
pol                              # equivalent to cd "/mnt/data/Pol Sci"

Alternatively, or in addition, you can enable the autocd option. This causes bash to interpret a directory in command position as the instruction to change to a directory.

shopt -s autocd
/some/where                 # equivalent to cd /some/where

Even if you do this, you might as well include cd in your uni alias, because the alias can't do anything else.

Another related feature worth mentioning is the CDPATH variable. It lets you define “quick directory change” locations. For example:

CDPATH=$HOME:/mnt/data
cd /some/where
cd uni

The command cd uni will try first to change to /some/where/uni, then if this doesn't exist it will try $HOME/uni, then if that doesn't exist it will try /mnt/data/uni. You may or may not like this feature because on the one hand it's sometimes convenient, but on the other hand it makes it easy to accidentally change into a directory you didn't intend and end up working on the wrong project.

1
  • Thank you, this is very thorough and helpful! I added the paths as variables and in addition created some aliased for fast cp operations. I'll consider adding the cdable_vars option. As you mentioned, I'll pass on the CDPATH variable because of the mentioned danger.
    – weygoldt
    Commented Mar 13, 2022 at 16:50
0

While the other answerers are correct that bash doesn't allow aliases for paths, you could consider switching to zsh, which has global aliases, which could be a path.

alias -g uni=/mnt/data/uni
cd uni

Works in zsh, but not bash.

1
  • Thank you! I am considering switching to zsh when I'm switching to arch soon.
    – weygoldt
    Commented Mar 13, 2022 at 16:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .