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
unipath=/mnt/data/uni
) and then using something likealias uni='cd "$unipath"'
? That would give you an alias for thecd
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 Mar 13 '22 at 10:15~/.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 Mar 13 '22 at 11:47~/.bash_aliases
makes a git pull of my dotfiles easier to handle since don't have to delete the native.bashrc
. – weygoldt Mar 13 '22 at 12:47~/.bash_aliases
? Huh.it didn't last time I was using it so I had thought it was an Ubuntu thing. – terdon Mar 13 '22 at 13:14.bash_aliases
file but there already was a snippet in the.bashrc
that looked for one, so I created it since it seemed to be convenient to offload it to another file. – weygoldt Mar 13 '22 at 16:52